实验3
实验三
任务1:
源代码:
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 return 0; 11 } 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:功能:将分数和等级转换;形参类型int,返回值类型char
问题2:有;输入成绩后,会输出所有等级
任务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 return 0; 11 } 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:将输入的数字每一位相加
问题2:能;分别使用迭代和递归
任务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 return 0; 11 } 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 }
输出结果:
问题1:输出了x的n次方
问题2:
任务4:
源代码:
1 #include <stdio.h> 2 #include <math.h> 3 4 int is_prime(int n) { 5 int i; 6 if (n <= 1) { 7 return 0; 8 } 9 for (i = 2; i <= sqrt(n); i++) { 10 if (n % i == 0) { 11 return 0; 12 } 13 } 14 return 1; 15 } 16 17 int main() { 18 int count = 0; 19 int i; 20 printf("100以内的孪生素数:\n"); 21 for (i = 1; i <= 100; i++) { 22 if (is_prime(i) && is_prime(i + 2)) { 23 printf("%d %d\n", i, i + 2); 24 count++; 25 } 26 } 27 printf("100以内的孪生素数共有%d个", count); 28 return 0; 29 }
输出结果:
任务5:
源代码:
1 #include <stdio.h> 2 3 int hanoi(int n, char from, char to, char pass) 4 { 5 if (n == 1) 6 { 7 printf("1 : %c -> %c\n", from, to); 8 return 1; 9 } 10 11 int num = hanoi(n - 1, from, pass, to) + 1; 12 printf("%d : %c -> %c\n", n, from, to); 13 num += hanoi(n - 1, pass, to, from); 14 15 return num; 16 } 17 18 int main() 19 { 20 int n; 21 char from, to, pass; 22 while (scanf("%d", &n) != EOF) 23 { 24 getchar(); 25 scanf("%c %c %c", &from, &pass, &to); 26 27 int num = hanoi(n, from, to, pass); 28 printf("Total moves: %d\n\n", num); 29 } 30 31 return 0; 32 }
输出结果:
任务6:
源代码:
迭代:
1 #include <stdio.h> 2 int main() 3 { 4 int n, m; 5 int ans; 6 7 while(scanf("%d%d", &n, &m)!= EOF){ 8 if (n < m) 9 { 10 printf("n = %d, m = %d, ans = 0\n\n", n, m); 11 continue; 12 } 13 14 ans = func(n, m); 15 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 16 } 17 18 return 0; 19 } 20 21 int func(int n, int m) 22 { 23 int ans = 1; 24 for (int i = 0; i < m; i++) 25 { 26 ans *= n--; 27 } 28 for (int i = 1; i <= m; i++) 29 { 30 ans /= i; 31 } 32 return ans; 33 }
递归:
1 #include <stdio.h> 2 int main() 3 { 4 int n, m; 5 int ans; 6 7 while(scanf("%d%d", &n, &m)!= EOF){ 8 if (n < m) 9 { 10 printf("n = %d, m = %d, ans = 0\n\n", n, m); 11 continue; 12 } 13 14 ans = func(n, m); 15 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 16 } 17 18 return 0; 19 } 20 21 int func(int n, int m) 22 { 23 if (m == n || m == 0) 24 return 1; 25 else 26 return func(n - 1, m) + func(n - 1, m - 1); 27 }
输出结果:
任务7:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 char print_charman(int n); 5 6 int main() { 7 int n; 8 printf("Enter n: "); 9 scanf("%d", &n); 10 print_charman(n); 11 return 0; 12 } 13 14 char print_charman(int n) 15 { 16 int t = 0; 17 for (int i = n; i >= 1; i--) 18 { 19 for (int j = 0; j < t; j++) 20 printf("\t"); 21 for (int j = 0; j < 2 * i - 1; j++) 22 printf(" o\t"); 23 printf("\n"); 24 for (int j = 0; j < t; j++) 25 printf("\t"); 26 for (int j = 0; j < 2 * i - 1; j++) 27 printf("<H>\t"); 28 printf("\n"); 29 for (int j = 0; j < t; j++) 30 printf("\t"); 31 for (int j = 0; j < 2 * i - 1; j++) 32 printf("I I\t"); 33 printf("\n"); 34 35 t++; 36 } 37 }
输出结果: