实验三
实验任务1:
源代码:
1 #include <stdio.h> 2 char score_to_grade(int score); 3 int main(){ 4 int score; 5 char grade; 6 7 while(scanf("%d",&score) !=EOF){ 8 grade = score_to_grade(score); 9 printf("分数:%d, 等级: %c\n\n", score, grade); 10 } 11 return 0; 12 } 13 char score_to_grade(int score){ 14 char ans; 15 16 switch(score/10){ 17 case 10: 18 case 9: ans = 'A'; break; 19 case 8: ans = 'B'; break; 20 case 7: ans = 'C'; break; 21 case 6: ans = 'D'; break; 22 default: ans = 'E'; 23 } 24 return ans; 25 }
回答问题:
1.功能是将分数转化为等级,形参类型是整型,返回值是字符型
2.有问题,字符型应使用‘’且缺少break
实验任务2:
源代码:
1 #include <stdio.h> 2 int sum_digits(int n); 3 int main(){ 4 int n; 5 int ans; 6 7 while(printf("Enter n: "), scanf("%d",&n) != EOF){ 8 ans = sum_digits(n); 9 printf("n = %d, ans = %d\n\n", n, ans); 10 } 11 12 return 0; 13 } 14 int sum_digits(int n){ 15 int ans = 0; 16 while(n != 0){ 17 ans += n%10; 18 n /=10; 19 } 20 21 return ans; 22 }
回答问题:
1.计算各个位数上的数之和
2.能实现同等效果,第一种是将各个位数拆分出来相加,第二种是利用递归逐个相加
实验任务3:
源代码:
1 #include <stdio.h> 2 int power(int x, int n); 3 int main(){ 4 int x, n; 5 int ans; 6 7 while(printf("Enter x and n: "), scanf("%d%d",&x,&n) !=EOF){ 8 ans = power(x,n); 9 printf("n = %d, ans = %d\n\n", n, ans); 10 } 11 return 0; 12 } 13 int power(int x, int n){ 14 int t; 15 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 }
回答问题:
1.计算x的n次方
2.是
实验任务4:
源代码:
1 #include <stdio.h> 2 #include <math.h> 3 4 int is_prime(int n); 5 6 int main(){ 7 int n,m=0; 8 9 printf("100以内的孪生素数:\n"); 10 for(n=1;n<=100;n++){ 11 if(is_prime(n)&&is_prime(n+2)){ 12 printf("%d %d\n",n,n+2); 13 m++; 14 } 15 } 16 printf("100以内的孪生素数共有%d个.\n",m); 17 } 18 19 int is_prime(int n){ 20 int i; 21 22 if(n<2){ 23 return 0; 24 } 25 else{ 26 for(i=2;i<=sqrt(n);i++) 27 if(n%i==0) 28 break; 29 if(i > sqrt(n)) 30 return 1; 31 else 32 return 0; 33 } 34 }
实验任务5:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void hanoi(unsigned int n,char from,char temp,char to); 5 void moveplate(unsigned int n,char from,char to); 6 7 int count; 8 int main(){ 9 unsigned int n; 10 while(scanf("%u",&n) !=EOF){ 11 hanoi(n,'A','B','C'); 12 printf("一共移动了%d次.\n",count); 13 count=0;} 14 system("pause"); 15 return 0; 16 } 17 void hanoi(unsigned int n,char from,char temp,char to){ 18 if(n==1){ 19 moveplate(n,from,to); 20 count += 1;} 21 else{ 22 hanoi(n-1,from,to,temp); 23 moveplate(n,from,to); 24 count += 1; 25 hanoi(n-1,temp,from,to); 26 } 27 28 } 29 30 void moveplate(unsigned int n,char from,char to){ 31 printf("%u:%c-->%c\n",n,from,to); 32 }
实验任务6:
源代码:
迭代:
1 #include <stdio.h> 2 int func(int n,int m); 3 4 int main(){ 5 int n,m; 6 int ans; 7 8 while(scanf("%d%d",&n,&m) !=EOF){ 9 ans=func(n,m); 10 printf("n = %d, m = %d, ans= %d\n\n",n,m,ans); 11 } 12 return 0; 13 } 14 15 int func(int n,int m){ 16 int a,b=1,c=1; 17 for(a=n;a>=n-m+1;a--){ 18 b*=a; 19 } 20 for(a=1;a<=m;a++){ 21 c*=a; 22 } 23 return b/c; 24 }
递归:
1 #include <stdio.h> 2 int func(int n,int m); 3 4 int main(){ 5 int n,m; 6 int ans; 7 8 while(scanf("%d%d",&n,&m) !=EOF){ 9 ans=func(n,m); 10 printf("n = %d, m = %d, ans= %d\n\n",n,m,ans); 11 } 12 return 0; 13 } 14 15 int func(int n,int m){ 16 if(m==0||m==n) 17 return 1; 18 else if(m>n) 19 return 0; 20 else 21 return func(n-1,m)+func(n-1,m-1); 22 }
实验任务7:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void print_charman(int n); 5 6 int main(){ 7 int n; 8 9 printf("Enter n: "); 10 scanf("%d",&n); 11 print_charman(n); 12 13 return 0; 14 } 15 16 void print_charman(int n){ 17 int i,j,k; 18 for(i=n*2;i>=1;i=i-2){ 19 for(j=1;j<i;j++){ 20 printf(" O \t"); 21 } 22 printf("\n"); 23 for(j=1;j<i;j++){ 24 printf("<H>\t"); 25 } 26 printf("\n"); 27 for(j=1;j<i;j++){ 28 printf("I I\t"); 29 } 30 printf("\n"); 31 printf("\n"); 32 } 33 printf("\t"); 34 }