实验3
实验任务1:
源代码:
1 #include <stdio.h> 2 3 char score_to_grade(int score); 4 5 int main() { 6 int score; 7 char grade; 8 9 while(scanf("%d", &score) != EOF) { 10 grade = score_to_grade(score); 11 printf("分数: %d, 等级: %c\n\n", score, grade); 12 } 13 14 return 0; 15 } 16 char score_to_grade(int score) { 17 char ans; 18 19 switch(score/10) { 20 case 10: 21 case 9: ans = 'A'; break; 22 case 8: ans = 'B'; break; 23 case 7: ans = 'C'; break; 24 case 6: ans = 'D'; break; 25 default: ans = 'E'; 26 } 27 28 return ans; 29 }
运行结果:
问题1:功能:将分数与等级挂钩,使相应分数输出相应等级 形参类型:整型 返回值类型:字符型
问题2:有;无break,程序会按照顺序依次执行,可能一次输出多个等级
实验任务2:
源代码:
1 #include <stdio.h> 2 3 int sum_digits(int n); 4 5 int main() { 6 int n; 7 int ans; 8 9 while(printf("Enter n: "), scanf("%d", &n) != EOF) { 10 ans = sum_digits(n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 14 return 0; 15 } 16 17 int sum_digits(int n) { 18 int ans = 0; 19 20 while(n != 0) { 21 ans += n % 10; 22 n /= 10; 23 } 24 25 return ans; 26 }
1 int sum_digits(int n) 2 { 3 if(n < 10) 4 return n; 5 return sum_digits(n/10) + n%10; 6 }
运行结果:
问题1:将n的各个位数进行相加
问题2:能;第一种是迭代,用n先对10取余,再取整 ,并循环相加来实现;而第二种是递归,在函数里再调用函数以实现各个位数的相加,当n<10时,函数终止,返回所有余数相加的值
实验任务3:
源代码:
1 #include <stdio.h> 2 3 int power(int x, int n); 4 5 int main() { 6 int x, n; 7 int ans; 8 9 while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) { 10 ans = power(x, n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 14 return 0; 15 } 16 17 int power(int x, int n) { 18 int t; 19 20 if(n == 0) 21 return 1; 22 else if(n % 2) 23 return x * power(x, n-1); 24 else { 25 t = power(x, n/2); 26 return t*t; 27 } 28 }
运行结果:
问题1:求x的n次方
问题2:是 ;递归模式: x * power(x, n-1)和 t = power(x, n/2)
数学公式模型:当n为偶数时,与x正负无关,求x的n次方;当n等于0时,输出1;当n为奇数时,x小于0,输出答案为负的,当x等于0时,输出0,当x大于0时,输出答案为正的
实验任务4:
源代码:
1 #include <stdio.h> 2 int is_prime(int n); 3 int main() 4 { 5 int i,count=0; 6 printf("100以内的孪生素数:\n") ; 7 for(i=0;i<101;i++) 8 { 9 if(is_prime(i)&&is_prime(i+2)) 10 { 11 printf("%d %d\n",i,i+2); 12 count+=1; 13 } 14 } 15 printf("100以内的孪生素数共有%d个",count); 16 return 0; 17 } 18 int is_prime(int n) 19 { 20 if (n < 2) 21 return 0; 22 for (int i = 2; i <= n / 2; i++) 23 { 24 if (n % i == 0) 25 return 0; 26 } 27 return 1; 28 }
运行结果:
实验任务5:
源代码:
1 #include <stdio.h> 2 #include<stdlib.h> 3 void hanoi(unsigned int n,char from,char temp,char to); 4 void moveplate(unsigned int n,char from,char to); 5 int count=0; 6 int main() 7 { 8 unsigned int n; 9 while(scanf("%u",&n)!=EOF) 10 { 11 count=0; 12 hanoi(n,'A','B','C'); 13 printf("一共移动了%d次\n",count); 14 } 15 return 0; 16 } 17 void hanoi(unsigned int n,char from,char temp,char to) 18 { 19 20 if(n==1) 21 { 22 moveplate(n,from,to); 23 } 24 25 else 26 { 27 hanoi(n-1,from,to,temp); 28 moveplate(n,from,to); 29 hanoi(n-1,temp,from,to); 30 } 31 } 32 void moveplate(unsigned int n,char from,char to) 33 { 34 printf("%u:%c-->%c\n",n,from,to); 35 count+=1; 36 }
运行结果:
试验任务6:
源代码:
迭代:
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() 4 { 5 int n, m; 6 int ans; 7 while(scanf("%d%d", &n, &m) != EOF) 8 { 9 ans = func(n, m); 10 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 11 } 12 return 0; 13 } 14 int func(int n, int m) 15 { 16 int ans=1; 17 if(m>n) 18 return 0; 19 else 20 { 21 for(int i=0;i<m;i++) 22 { 23 ans*=n; 24 n-=1; 25 } 26 for(int i=1;i<=m;i++) 27 { 28 ans/=i; 29 } 30 return ans; 31 } 32 }
运行结果:
递归:
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() 4 { 5 int n, m; 6 int ans; 7 while(scanf("%d%d", &n, &m) != EOF) 8 { 9 ans = func(n, m); 10 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 11 } 12 return 0; 13 } 14 int func(int n, int m) 15 { 16 if(m>n) 17 return 0; 18 else if (m == n || m == 0) 19 return 1; 20 else 21 { 22 return func(n - 1, m) + func(n - 1, m - 1); 23 } 24 }
运行结果:
实验任务7:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 void print_charman(int n); 4 int main() 5 { 6 int n; 7 printf("Enter n: "); 8 scanf("%d", &n); 9 print_charman(n); 10 return 0; 11 } 12 void print_charman(int n) 13 { 14 int m = 0; 15 for (int i = n; i >= 1; i--) 16 { 17 for (int j = 0; j < m; j++) 18 { 19 printf("\t"); 20 } 21 for (int j = 0; j < 2 * i - 1; j++) 22 { 23 printf(" o\t"); 24 } 25 printf("\n"); 26 for (int j = 0; j < m; j++) 27 { 28 printf("\t"); 29 } 30 for (int j = 0; j < 2 * i - 1; j++) 31 { 32 printf("<H>\t"); 33 } 34 printf("\n"); 35 for (int j = 0; j < m; j++) 36 { 37 printf("\t"); 38 } 39 for (int j = 0; j < 2 * i - 1; j++) 40 { 41 printf("I I\t"); 42 } 43 printf("\n"); 44 m++; 45 } 46 }
运行结果:
实验总结:
对汉诺塔程序有更深的理解
基本熟悉迭代和递归间的关系和区别