实验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 17 char score_to_grade(int score) { 18 char ans; 19 20 switch(score/10) { 21 case 10: 22 case 9: ans = 'A'; break; 23 case 8: ans = 'B'; break; 24 case 7: ans = 'C'; break; 25 case 6: ans = 'D'; break; 26 default: ans = 'E'; 27 } 28 29 return ans; 30 }
1 判断分数等级 整形 字符形
2 有问题 A到D为字符串型 E为字符型 没有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 让一个数的各个位数相加
2 能 第一种通过对一个数取余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 是
任务4
1 #include <stdio.h> 2 #include <math.h> 3 4 int is_prime(int num); 5 6 int main() { 7 printf("100以内的孪生素数:\n"); 8 int count = 0; 9 for (int i = 2; i < 100; i++) { 10 if (is_prime(i) && is_prime(i + 2)) { 11 printf("%d %d \n", i, i + 2); 12 count++; 13 } 14 } 15 printf("100以内的李生素数共有: %d 对\n", count); 16 return 0; 17 } 18 19 int is_prime(int num) { 20 if (num <= 1) return 0; 21 if (num == 2) return 1; 22 if (num % 2 == 0) return 0; 23 for (int i = 3; i <= sqrt(num); i += 2) { 24 if (num % i == 0) { 25 return 0; 26 } 27 } 28 return 1; 29 }
任务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 moves=0; 6 int main(){ 7 8 unsigned int n; 9 while(scanf("%u",&n)!=EOF){ 10 moves=0; 11 hanoi(n,'A','B','C'); 12 printf("一共移动了%d次\n\n",moves); 13 14 15 } 16 17 return 0; 18 } 19 void hanoi(unsigned int n,char from,char temp,char to){ 20 21 if(n==1){ 22 moves++; 23 moveplate(n,from,to); 24 } 25 26 27 else 28 { 29 30 hanoi(n-1,from,to,temp); 31 moves++; 32 moveplate(n,from,to); 33 hanoi(n-1,temp,from,to); 34 35 } 36 37 } 38 void moveplate(unsigned int n,char from,char to){ 39 printf("%u:%c-->%c\n",n,from,to); 40 41 }
任务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 if (m > n) return 0; 18 int result = 1; 19 for (int i = 1; i <= m; ++i) { 20 result *= (n - m + i); 21 result /= i; 22 } 23 return result; 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 13 return 0; 14 } 15 16 int func(int n, int m) { 17 if (m == 0 || n == m) return 1; 18 if (m > n) 19 return 0; 20 return func(n - 1, m) + func(n - 1, m - 1); 21 }
任务7
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void print_charman(int n); 5 6 7 int main() { 8 int n; 9 10 printf("Enter n: "); 11 scanf("%d", &n); 12 print_charman(n); 13 14 return 0; 15 } 16 17 void print_charman(int n){ 18 int x = 0,i,j; 19 for (i = n; i >= 1; i--) 20 { 21 for (j = 0; j <= x; j++) 22 { 23 printf("\t"); 24 } 25 for (j = 0; j < 2 * i - 1; j++) 26 { 27 printf(" O\t"); 28 } 29 printf("\n"); 30 for (j = 0; j <= x; j++) 31 { 32 printf("\t"); 33 } 34 for (j = 0; j < 2 * i - 1; j++) 35 { 36 printf("<H>\t"); 37 } 38 printf("\n"); 39 for (j = 0; j <= x; j++) 40 { 41 printf("\t"); 42 } 43 for (j = 0; j < 2 * i - 1; j++) 44 { 45 printf("I I\t"); 46 } 47 printf("\n"); 48 x++; 49 } 50 51 }