实验3 C语言函数应用编程
实验任务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 return ans; 30 }
运行结果:
回答问题:
问题1:判断分数等级:90~100为A,80~90为B,70~80为C,60~70为D,低于60为E 形参类型为整型,返回值类型为字符型
问题2:有问题,若不加入break,则判断等级后,将继续赋值剩余的等级,最后结果一定为E
如图:
实验任务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:将输入的数字各数位上的数相加,即123,为1+2+3=6
问题2:可以,原代码是迭代,补充的为递归
实验任务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次幂(x,n均为整数)
问题2:是,如图
实验任务4:
源代码:
1 #include <stdio.h> 2 3 int is_prime(int n) { 4 if (n <= 1) return 0; 5 for (int i = 2; i * i <= n; i++) 6 { 7 if (n % i == 0) return 0; 8 } 9 return 1; 10 } 11 12 int main() { 13 int count = 0; 14 15 printf("100 以内的孪生素数:\n"); 16 for (int i = 2; i < 100; i++) 17 { 18 if (is_prime(i) && is_prime(i + 2)) 19 { 20 printf("%d, %d\n", i, i + 2); 21 count++; 22 } 23 } 24 25 printf("一百以内的孪生素数共有%d个", count); 26 27 return 0; 28 }
运行结果:
实验任务5:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 unsigned int move_count = 0; 5 6 void moveplate(unsigned int n, char from, char to); 7 void hanoi(unsigned int n, char from, char temp, char to); 8 9 int main() { 10 unsigned int n; 11 printf("请输入盘子数目:"); 12 scanf_s("%u", &n); 13 hanoi(n, 'A', 'B', 'C'); 14 printf("一共移动了 %u 个盘子。\n", move_count); 15 system("pause"); 16 return 0; 17 } 18 19 void hanoi(unsigned int n, char from, char temp, char to) { 20 if (n == 1) { 21 moveplate(n, from, to); 22 } 23 else { 24 hanoi(n - 1, from, to, temp); 25 26 moveplate(n, from, to); 27 28 hanoi(n - 1, temp, from, to); 29 } 30 } 31 32 void moveplate(unsigned int n, char from, char to) { 33 printf("%u: %c -> %c\n", n, from, to); 34 move_count++; 35 }
运行结果:
实验任务6:
源代码:
迭代:
1 #include <stdio.h> 2 3 int func(int n, int m); 4 5 int main() { 6 int n, m; 7 int ans; 8 while (scanf_s("%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 return 0; 13 } 14 15 int func(int n, int m) { 16 17 if (m > n) return 0; 18 if (m == 0 || m == n) return 1; 19 20 int ans = 1; 21 22 for (int j = 0; j < m; j++) { 23 ans *= n - j; 24 ans /= (j + 1); 25 } 26 return ans; 27 }
运行结果:
递归:
1 #include <stdio.h> 2 3 int func(int n, int m); 4 5 int main() { 6 int n, m; 7 int ans; 8 while (scanf_s("%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 return 0; 13 } 14 15 int func(int n, int m) { 16 if (m > n) 17 return 0; 18 if (m == 0 || m == n) 19 return 1; 20 return func(n - 1, m - 1) + func(n - 1, m); 21 }
运行结果:
实验任务7:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void print_charman(int n); 5 6 int main() { 7 int n; 8 9 printf("Enter n: "); 10 scanf_s("%d", &n); 11 print_charman(n); 12 13 return 0; 14 } 15 16 void print_charman(int n) { 17 for (int i = n; i > 0; i--) { 18 for (int a = 0; a < n - i; a++) { 19 printf("\t"); 20 } 21 22 for (int j = 0; j < 2 * i - 1; j++) { 23 printf(" O \t"); 24 } 25 printf("\n"); 26 27 for (int a = 0; a < n - i; a++) { 28 printf("\t"); 29 } 30 for (int j = 0; j < 2 * i - 1; j++) { 31 printf("<H>\t"); 32 } 33 printf("\n"); 34 35 for (int a = 0; a < n - i; a++) { 36 printf("\t"); 37 } 38 for (int j = 0; j < 2 * i - 1; j++) { 39 printf("I I\t"); 40 } 41 printf("\n\n"); 42 } 43 }
运行结果:
实验总结:
1.实验7中居中倒三角的实现主要靠空格的增加(可用for循环)
2.组合数的程序也可以用数组实现,如下:
1 #include <stdio.h> 2 3 int combination(int n, int m); 4 5 int main() { 6 int n, m; 7 8 printf("请输入 n 和 m:"); 9 scanf("%d %d", &n, &m); 10 11 int result = combination(n, m); 12 printf("C(%d, %d) = %d\n", n, m, result); 13 14 return 0; 15 } 16 17 int combination(int n, int m) { 18 if (m > n) return 0; 19 if (m == 0 || m == n) return 1; 20 21 int C[m + 1]; 22 for (int i = 0; i <= m; i++) { 23 C[i] = 0; 24 } 25 26 C[0] = 1; 27 28 for (int i = 1; i <= n; i++) { 29 for (int j = (i < m ? i : m); j > 0; j--) { 30 C[j] = C[j] + C[j - 1]; 31 } 32 } 33 34 return C[m]; 35 }