实验三
实验一:
源代码:
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 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 }
运行结果:
问题:函数功能是将分数与等级挂钩,进行转换。形参是int,返回值是char。
实验二:
源代码:
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 { 8 ans=sum_digits(n); 9 printf("n=%d,ans=%d\n\n",n,ans); 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 }
运行结果:
问题:函数是计算所有数字之和
可以实现,改后是利用递归的方式计算
实验三:
源代码:
1 #include<stdio.h> 2 int power(int x,int n); 3 int main() 4 { 5 int x,n; 6 int ans; 7 while(printf("Enter x and n"),scanf("%d%d",&x,&n)!=EOF){ 8 ans=power(x,n); 9 printf("n=%d,ans=%d%d\n\n",n,ans); 10 } 11 return 0; 12 } 13 int power(int x,int n){ 14 int t; 15 if(n==0) 16 return 1; 17 else if(n%2) 18 return x*power(x,n-1); 19 else{ 20 t=power(x,n/2); 21 return t*t; 22 } 23 } 24 实验7:#include<stdio.h> 25 #include<stdlib.h> 26 int print_char man(int n); 27 int main(){ 28 it n; 29 printf("Enter n:"); 30 scanf("%d",&n); 31 print_charman(n); 32 return 0; 33 } 34 int printf_charman(int n) 35 { 36 int i=0; 37 for(;i<n;i++) 38 { 39 printf(" O"); 40 printf("<H>"); 41 printf("I I"); 42 } 43 44 }
运算结果:
问题:函数用于计算一个数的n次方
实验四:
源代码:
1 #include <stdio.h> 2 #include <math.h> 3 int is_prime(int n); 4 int main() { 5 int count = 0; 6 printf("100以内的孪生素数:\n"); 7 for (int i = 2; i <= 98; i++) { 8 if (is_prime(i) && is_prime(i + 2)) { 9 printf("%d %d\n", i, i + 2); 10 count++; 11 } 12 } 13 printf("100以内的孪生素数共有%d个\n", count); 14 return 0; 15 } 16 int is_prime(int n) { 17 if (n <= 1) return 0; 18 if (n <= 3) return 1; 19 if (n % 2 == 0 || n % 3 == 0) return 0; 20 for (int i = 5; i <= sqrt(n); i += 6) { 21 if (n % i == 0 || n % (i + 2) == 0) return 0; 22 } 23 return 1; 24 }
运算结果:
实验五:
源代码:
1 #include <stdio.h> 2 void hanoi(int n, char from_rod, char to_rod, char aux_rod); 3 int main() { 4 int n; 5 while (scanf("%d", &n) != EOF) { 6 printf("%d\n", n); 7 int moves = 0; 8 hanoi(n, 'A', 'C', 'B'); 9 printf("一共移动了%d次.\n", (1 << n) - 1); 10 } 11 return 0; 12 } 13 void hanoi(int n, char from_rod, char to_rod, char aux_rod) { 14 if (n == 1) { 15 printf("1:%c --> %c\n", from_rod, to_rod); 16 return; 17 } 18 hanoi(n - 1, from_rod, aux_rod, to_rod); 19 printf("1:%c --> %c\n", from_rod, to_rod); 20 hanoi(n - 1, aux_rod, to_rod, from_rod); 21 }
运行结果:
实验六:
源代码:
1 #include <stdio.h> 2 int func_iterative(int n, int m); 3 int func_recursive(int n, int m); 4 5 int main() { 6 int n, m; 7 int ans; 8 while (scanf("%d %d", &n, &m) != EOF) { 9 ans = func_iterative(n, m); 10 printf("n=%d, m=%d, ans=%d\n\n", n, m, ans); 11 } 12 return 0; 13 } 14 int func_iterative(int n, int m) { 15 if (m > n) return 0; 16 int result = 1; 17 for (int i = 1; i <= m; ++i) { 18 result *= (n - i + 1); 19 result /= i; 20 } 21 return result; 22 } 23 int func_recursive(int n, int m) { 24 if (m == 0 || m == n) return 1; 25 if (m > n) return 0; 26 return func_recursive(n - 1, m - 1) + func_recursive(n - 1, m); 27 }
运行结果:
实验七:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 char print_charman(int n); 4 int main() { 5 int n; 6 printf("Enter n: "); 7 scanf("%d", &n); 8 print_charman(n); 9 return 0; 10 } 11 char print_charman(int n) 12 { 13 int t = 0; 14 for (int i = n; i >= 1; i--) 15 { 16 for (int j = 0; j < t; j++) 17 printf("\t"); 18 for (int j = 0; j < 2 * i - 1; j++) 19 printf(" o\t"); 20 printf("\n"); 21 for (int j = 0; j < t; j++) 22 printf("\t"); 23 for (int j = 0; j < 2 * i - 1; j++) 24 printf("<H>\t"); 25 printf("\n"); for (int j = 0; j < t; j++) 26 printf("\t"); 27 for (int j = 0; j < 2 * i - 1; j++) 28 printf("I I\t"); 29 printf("\n"); 30 t++; 31 } 32 }
运行结果: