翁凯C语言--------第五周编程练习-------念整数和素数和
程序员文章站
2022-04-03 22:34:29
...
第五周编程练习-------念整数和素数和
#include<stdio.h>
int main(){
int count=0,sum=0;
int isPrime;
int m,n,i,j;
scanf("%d %d",&n,&m);
if(n>=0&&m<=200&&n<=m){
for(i=2;;i++){
isPrime=1;
for(j=2;j<i;j++)
if(i%j==0) {
isPrime=0;
break;
}
if(isPrime==1) count++;
if(count>=n&&count<=m&&isPrime==1)
sum+=i;
if(count==m) break;
}
printf("%d",sum);
}
return 0;
}
#include<stdio.h>
#include<math.h>
int main(){
int number,n,digit=0,x;
scanf("%d",&number);
n=number;
if(n>=-100000&&n<=100000){
if(n<0) {
printf("fu ");
number=n=-n;}
if(n==0) printf("ling"); //对特殊0做处理
while(number>0){
number/=10;
digit++;
}
x=pow(10,digit-1);
int i,shu;
for(i=0;i<digit;i++){
shu=n/x%10;
x=x/10;
switch(shu){
case 0:
printf("ling"); break;
case 1:
printf("yi"); break;
case 2:
printf("er"); break;
case 3:
printf("san"); break;
case 4:
printf("si"); break;
case 5:
printf("wu"); break;
case 6:
printf("liu"); break;
case 7:
printf("qi"); break;
case 8:
printf("ba"); break;
case 9:
printf("jiu"); break;
}
if(i!=digit-1) printf(" ");
}
}
return 0;
}
方法二:不使用switch,用数组存储
#include<stdio.h>
#include<math.h>
int main(){
int number,n,digit=0,x;
char shubiao[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; //5是因为加一个串结束符,这样才可以存储
scanf("%d",&number);
n=number;
if(n>=-100000&&n<=100000){
if(n<0) {
printf("fu ");
number=n=-n;}
if(n==0) printf("ling"); //对特殊0做处理
while(number>0){
number/=10;
digit++;
}
x=pow(10,digit-1);
int i,shu;
for(i=0;i<digit;i++){
shu=n/x%10;
x=x/10;
printf("%s",shubiao[shu]);
if(i!=digit-1) printf(" ");
}
}
return 0;
}
上一篇: 在Pycharm中import torch报错的解决办法
下一篇: pandas学习——综合练习题