实验3
TASK 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 17 18 char score_to_grade(int score) { 19 char ans; 20 21 switch(score/10) { 22 case 10: 23 case 9: ans = 'A'; break; 24 case 8: ans = 'B'; break; 25 case 7: ans = 'C'; break; 26 case 6: ans = 'D'; break; 27 default: ans = 'E'; 28 } 29 30 return ans; 31 }
问题1:通过对score除以10划分成绩为90+、80+、70+等等的不同层次;形参是整形,返回值是字符
问题2:有问题;删去break后,程序无法在完成score-〉grade后终止,可能导致串线(直接跳转到default);A、B、C、D是字符而不是字符串,应该改用单引号,可能导致报错无法运行
TASK 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 18 int sum_digits(int n) { 19 int ans = 0; 20 21 while(n != 0) { 22 ans += n % 10; 23 n /= 10; 24 } 25 26 return ans; 27 }
问题1:计算输入数字(两位数)各位之和
问题2:能实现同等效果;上述方法是通过整除先后获得十位和个位(方法相同),另一个方法是先整除得到十位,再取模得到个位(方法不同)
TASK 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 18 int power(int x, int n) { 19 int t; 20 21 if(n == 0) 22 return 1; 23 else if(n % 2) 24 return x * power(x, n-1); 25 else { 26 t = power(x, n/2); 27 return t*t; 28 } 29 }
问题1:计算x的n次方
问题2:是递归函数;递归模式:a)当指数为0时答案为1输出,b)当指数为偶数时答案为x与x的(n-1)次幂的乘积输出,c)当指数为奇数时答案为(x的二分之n)平方输出
TASK 4
1 #include <stdio.h> 2 3 int is_prime(int n); 4 5 int main() 6 { 7 printf("100以内的孪生素数:\n"); 8 int n,sum = 0; 9 for(n = 1;n <= 100;n++) 10 { 11 if(is_prime(n)&&is_prime(n+2)) 12 { 13 printf("%d %d\n",n,n+2); 14 sum += 1; 15 } 16 } 17 printf("100以内的孪生素数有%d个\n",sum); 18 return 0; 19 } 20 21 int is_prime(int n) 22 { 23 if(n == 1) 24 return 0; 25 int i; 26 for(i = 2;i <= n/2;i++) 27 { 28 if(n%i == 0) 29 return 0; 30 } 31 if(i > n/2) 32 return 1; 33 }
TASK 5
1 #include<stdio.h> 2 3 void Hanoi(int n, char A, char B, char C) 4 5 { 6 if (n >= 2) 7 { 8 Hanoi(n - 1, A, C, B); 9 printf("%c -> %c\n", A, B); 10 Hanoi(n - 1, B, A, C); 11 } 12 else 13 { 14 printf("%c -> %c\n", A, C); 15 } 16 } 17 18 19 int main() 20 { 21 int n = 0; 22 scanf("%d", &n); 23 Hanoi(n, 'A', 'B', 'C'); 24 return 0; 25 }
TASK 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 13 return 0; 14 } 15 16 int func(int n,int m) 17 { 18 int ans = 1,i; 19 if (m == 0||m == n) 20 return 1; 21 else if (m > n) 22 return 0; 23 else 24 { 25 for(i = 1;i <= m;++i) 26 { 27 if(n-i < 0) 28 return 0; 29 ans = ans * (n-i+1) / i; 30 } 31 return ans; 32 } 33 }
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 13 return 0; 14 } 15 16 int func(int n,int m) 17 { 18 int ans = 1; 19 if (m == 0||m == n) 20 return 1; 21 else if (m > n) 22 return 0; 23 else 24 { 25 return func(n-1,m) + func(n-1,m-1); 26 } 27 }
TASK 7
1 #include <stdio.h> 2 #include <stdlib.h> 3 void print_charman(int n); 4 5 int main() 6 { 7 int n; 8 printf("Enter n: "); 9 10 scanf("%d", &n); 11 print_charman(n); 12 13 return 0; 14 } 15 16 void print_charman(int n) 17 { 18 int i,j,k; 19 for(i = n;i >= 1;i--){ 20 for(k = i;k < n;k++) 21 printf("\t"); 22 23 24 for(j = 1;j <= 2*i-1;j++) 25 { 26 printf(" o\t"); 27 } 28 printf("\n"); 29 30 31 32 for(k = i;k < n;k++) 33 printf("\t"); 34 35 36 for(j = 1;j <= 2*i-1;j++) 37 { 38 printf("<H>\t"); 39 } 40 printf("\n"); 41 42 43 44 for(k = i;k < n;k++) 45 printf("\t"); 46 47 48 for(j = 1;j <= 2*i-1;j++) 49 { 50 printf("I I\t"); 51 } 52 printf("\n"); 53 54 55 printf("\n"); 56 } 57 }