实验3
1
1 #include<stdio.h> 2 char score(int s); 3 int main() 4 { 5 int s; 6 char a; 7 while (scanf("%d", &s) != EOF) 8 { 9 a = score(s); 10 printf("分数:%d", s); 11 printf("等级:%c\n", a); 12 } 13 return 0; 14 } 15 16 char score(int s) 17 { 18 char a; 19 switch (s / 10) 20 { 21 case 10: 22 case 9:a = 'A'; break; 23 case 8:a = 'B'; break; 24 case 7:a = 'C'; break; 25 case 6:a = 'D'; break; 26 default:a = 'E'; 27 } 28 return a; 29 }
问题1:将分数转化为等级字母,参数类型是整数,返回值类型是字符型
问题2:有问题,如果不加不break,则一直输出,如当输入100则输出ABCDE
2
1 #include<stdio.h> 2 int sumd(int n); 3 int main(){ 4 int n; 5 int ans = 0; 6 while (printf("Enter n:"), scanf("%d", &n) != EOF) 7 { 8 ans = sumd(n); 9 printf("n=%d,ans=%d\n\n", n, ans); 10 } 11 return 0; 12 } 13 int sumd(int n) 14 { 15 int ans = 0; 16 while (n != 0) 17 { 18 ans += n % 10; 19 n /= 10; 20 } 21 return ans; 22 }
问题1:将一个数各个位置数字求和
问题2:可以得到各个位数和,但也同时将n变成逆过来排列
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 int power(int x, int n) { 13 int t; 14 if (n == 0) 15 return 1; 16 else if (n % 2) 17 return x * power(x, n - 1); 18 else { 19 t = power(x, n / 2); 20 return t * t; 21 } 22 }
问题1:该函数计算一个数的x次方
问题2
4
1 #include<stdio.h> 2 int isprime(int n) 3 { 4 int i; 5 if (n == 1) 6 { 7 return 0; 8 } 9 for (i = 2; i*i <= n ; i++) 10 { 11 if (n % i == 0) 12 { 13 return 0; 14 } 15 } 16 return 1; 17 } 18 int main() 19 { 20 int i, s; 21 s = 0; 22 for (i = 1; i < 100; i++) 23 { 24 if (isprime(i) && isprime(i + 2)) 25 { 26 printf("%d %d\n", i, i + 2); 27 s++; 28 } 29 } 30 printf("一共%d个孪生素数", s); 31 return 0; 32 }
5
1 #include<stdio.h> 2 int t = 0; 3 void hanoi(unsigned int n, char from, char temp, char to); 4 int main() 5 { 6 unsigned int n; 7 while (scanf("%u", &n) != EOF) 8 { 9 hanoi(n, 'A', 'B', 'C'); 10 printf("一共移动了%d次\n", t); 11 t = 0; 12 } 13 return 0; 14 } 15 void hanoi(unsigned int n, char from, char temp, char to) 16 { 17 if (n == 1) { 18 printf("%u:%c-->%c\n", n, from, to); 19 t++; 20 } 21 else 22 { 23 hanoi(n - 1, from, to, temp); 24 printf("%u:%c-->%c\n", n, from, to); 25 t++; 26 hanoi(n - 1, temp, from, to); 27 } 28 }
6 迭代
1 #include <stdio.h> 2 int func(int n, int m); // 函数声明 3 int main() { 4 int n, m; 5 int ans; 6 while (scanf("%d%d", &n, &m) != EOF) { 7 ans = func(n, m); // 函数调用 8 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 9 } 10 return 0; 11 } 12 int func(int n, int m) 13 { 14 int a, b, c, ans = 1; 15 for (a = 1; a <= n; a++) 16 { 17 ans = ans * a; 18 } 19 for (b = 1; b <= m; b++) 20 { 21 ans = ans / b; 22 } 23 for (c = 1; c <= (n - m); c++) 24 { 25 ans = ans / c; 26 } 27 return ans; 28 29 }
1 #include <stdio.h> 2 int func(int n, int m); // 函数声明 3 int main() { 4 int n, m; 5 int ans; 6 while (scanf("%d%d", &n, &m) != EOF) { 7 ans = func(n, m); // 函数调用 8 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 9 } 10 return 0; 11 } 12 int func(int n, int m) 13 { 14 if (m > n) 15 { 16 return 0; 17 } 18 else if (m == 0) 19 { 20 return 1; 21 } 22 else return (func(n - 1, m) + func(n - 1, m - 1)); 23 }
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 i, m, t,a; 14 for (i = n; i >= 1; i--) 15 { 16 printf("\n"); 17 t = 2 * i - 1; 18 for (a = 0; a < n - i; a++) 19 { 20 printf("\t"); 21 } 22 for (m = 1; m <= t; m++) 23 { 24 printf(" O \t"); 25 } 26 printf("\n"); 27 for (a = 0; a < n - i; a++) 28 { 29 printf("\t"); 30 } 31 for (m = 1; m <= t; m++) 32 { 33 printf("<H>\t"); 34 } 35 printf("\n"); 36 for (a = 0; a < n - i; a++) 37 { 38 printf("\t"); 39 } 40 for (m = 1; m <= t; m++) 41 { 42 printf("I I\t"); 43 } 44 } 45 }