任务一
源代码
1 #include<stdio.h> 2 char score_to_grade(int score); 3 int main(){ 4 int score; 5 char grade; 6 while (scanf("%d",&score)!=EOF){ 7 grade=score_to_grade(score); 8 printf("分数:%d,等级:%c\n\n",score,grade); 9 10 } 11 return 0; 12 } 13 char score_to_grade(int score){ 14 char ans; 15 switch(score/10){ 16 case 10: 17 case 9: ans='A';break; 18 case 8: ans='B';break; 19 case 7: ans='C';break; 20 case 6: ans='D';break; 21 default: ans='E'; 22 } 23 return ans; 24 }
问题答案
问1函数的作用是将100分制的成绩从高到低划分等级ABCDE(不及格) 形参类型和返回值类型都是char字符
问2以另一种形式有问题,输入100以内的数,除以10,最大只有9,它会只有E
任务2
1 #include<stdio.h> 2 int sum_digits(int n); 3 int main(){ 4 int n; 5 int ans; 6 while(printf("Enter n:"),scanf("%d",&n)!=EOF){ 7 ans=sum_digits(n); 8 printf("n=%d,ans=%d\n\n",n,ans); 9 10 } 11 return 0; 12 } 13 int sum_digits(int n){ 14 int ans=0; 15 while(n!=0){ 16 ans+=n%10; 17 n/=10; 18 } 19 return ans; 20 }
问题答案
函数的功能是计算各位数的和
能实现,这个是循环迭代,后者是递归
任务3
源代码
1 #include<stdio.h> 2 int power(int x,int n); 3 int main(){ 4 int x,n; 5 int ans; 6 while(printf("enter x and n:"),scanf("%d%d",&x,&n)!=EOF){ 7 ans=power(x,n); 8 printf("n=%d,ans=%d\n\n",n,ans); 9 10 } 11 return 0; 12 13 } 14 int power(int x,int n){ 15 int t; 16 if(n==0) 17 return 1; 18 else if(n%2) 19 return x*power(x,n-1); 20 else{ 21 t= power(x,n/2); 22 return t*t; 23 } 24 }
问题答案
函数power的功能是算出x的n次方
是递归函数 数学模型是幂运算
任务4
源代码
1 #include<stdio.h> 2 int is_prime(int n); 3 int main(){ 4 int n,count=0; 5 printf("100 以内的孪生素数:"); 6 for(n=2;n<=100;n++){ 7 if(is_prime(n)&&is_prime(n+2)){ 8 count++; 9 printf("%d %d\n",n,n+2); 10 } 11 12 } 13 printf("100以内的孪生素数共有%d 个" ,count); 14 15 return 0; 16 } 17 int is_prime(int n){ 18 int i,a=0; 19 for(i=2;i*i<=n;i++){ 20 if(n%i==0) 21 return 0; 22 } 23 return 1; 24 }
问题答案
任务5
源代码
1 #include<stdio.h> 2 void hanio(int n,int *count,char from,char temp,char to); 3 void move(int n,char from,char to); 4 5 int main(){ 6 int n,count=0; 7 while(scanf("%u",&n)!=EOF){ 8 hanio(n,&count,'A','B','c'); 9 printf("一共移动了%d次\n",count); 10 count=0; 11 system("pause"); 12 } 13 return 0; 14 } 15 void hanio(int n,int *count,char from,char temp,char to){ 16 if(n==1){ 17 move(1,from,to); 18 (*count)++; 19 } 20 else{ 21 hanio(n-1,count,from,to,temp); 22 move(n,from,to); 23 (*count)++; 24 hanio(n-1,count,temp,from,to); 25 } 26 } 27 void move(int n,char from,char to){ 28 printf("%u:%c-->%c\n",n,from,to); 29 }
问题答案
任务6
源代码
1 #include<stdio.h> 2 int function(int n,int m); 3 int main(){ 4 int n,m; 5 int ans=0; 6 while(scanf("%d%d",&n,&m)!=EOF){ 7 ans=function(n,m); 8 printf("n=%d ,m=%d,ans=%d\n\n",n,m,ans); 9 10 } 11 return 0; 12 } 13 int function(int n,int m){ 14 int bcs=1; 15 int i,t; 16 int cs=1; 17 if(m>n||m<0||n<0){ 18 return 0; 19 } 20 21 else{ 22 for(i=n;i>=n-m+1;i--) 23 {bcs *=i; 24 } 25 for(t=1;t<=m;t++) 26 {cs *=t; 27 } 28 29 } 30 31 return bcs/cs; 32 }
1 #include<stdio.h> 2 int function(int n,int m); 3 int main(){ 4 int n,m; 5 int ans=0; 6 while(scanf("%d%d",&n,&m)!=EOF){ 7 ans=function(n,m); 8 printf("n=%d ,m=%d,ans=%d\n\n",n,m,ans); 9 10 } 11 return 0; 12 } 13 int function(int n,int m){ 14 if(m==0||n==m) 15 return 1; 16 else if(m>n||n<0||m<0) 17 return -1; 18 else 19 return function(n-1,m)+function(n-1,m-1); 20 }
问题答案
标签:count,10,return,int,char,实验,ans From: https://www.cnblogs.com/cpblogs/p/18497996