实验三

任务1:

 1 #include <stdio.h>
 2 char score_to_grade(int score); // 函数声明
 3 int main() {
 4     int score;
 5     char grade;
 6     while (scanf_s("%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 }

 

问题一:函数的功能是根据输入的分数返回分数所对应的等级,形参类型是整形,返回值是字符型;

问题二:有问题,没有break,程序会将满足条件的代码以及后续代码全部执行;

任务二:

函数一:

 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_s("%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 #include <stdio.h>
 2 int sum_digits(int n); // 函数声明
 3 int main() {
 4     int n;
 5     int ans;
 6     while (printf("Enter n: "), scanf_s("%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     if (n < 10)
15         return n;
16     return sum_digits(n / 10) + n % 10;
17 }

 

问题一:函数的功能是求输入的数字的各个数位的数字的和;

问题二:可以,函数1使用while循环(迭代),在循环内求出各个数位的和;

函数二使用递归,返回最后一个数位的值和函数,不断递归,直到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 // 函数定义
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 }

 

问题一:函数的功能是计算 x 的 n次方;

问题二:f(n)={ 1, n=0

                          { x *f(n-1), n为奇数

                          { x*x*f(n-1)*f(n-1)*f(n/2), n为偶数

任务四:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 
 5 
 6 int main() {
 7     int a[100] = { 0 };
 8     int f = 0;
 9     for (int t = 1; t < 100; t++) {
10         int count = 0;
11         int s = 0;
12         for (int s = 1; s <= sqrtl(t); s++) {
13             if (t % s == 0) {
14                 count++;
15             }
16         }
17         if (count == 1) {
18             a[f] = t;
19             f++;
20         }
21     }
22     printf("100以内的孪生素数\n");
23     int x = 0;
24     int e = 0;
25     int l, y;
26     for (; e < 26; e++) {
27         if ((a[e + 1] - a[e]) == 2) {
28             printf("%d %d\n",a[e],a[e+1]);
29             x++;
30         }
31     }
32     printf("100以内的孪生素数共有%d个\n", x);
33     return 0;
34 
35 }

 

任务五:

 1 void hanio(unsigned int n, char from, char temp, char to);
 2 void moveplate(unsigned int n, char from, char to);
 3 int t = 0;
 4 int main() {
 5     unsigned int n;
 6     while(scanf_s("%u", &n) !=EOF) {
 7         t = 0;
 8         hanio(n, 'A', 'B', 'C');
 9         printf("\n一共移动了%d次\n", t);
10     }
11     return 0;
12 }
13 
14 void hanio(unsigned int n, char from, char temp, char to) {
15     if (n == 1) {
16         moveplate(n, from, to);
17     }
18     else {
19         hanio(n - 1, from, to, temp);
20         moveplate(n, from, to);
21         hanio(n - 1, temp, from, to);
22     }
23 
24 }
25 void moveplate(unsigned int n, char from, char to) {
26     printf("%u:%c-->%c \n", n, from, to);
27     t++;
28 }

 

任务六:

迭代

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int jiecheng(int t);
 5 
 6 int main() {
 7     int ans;
 8     int n, m;
 9     while (scanf_s("%d%d", &n, &m) != EOF) {
10         if (m > n) {
11             ans = 0;
12         }
13         else {
14             ans = jiecheng(n) / (jiecheng(m) * jiecheng(n - m));
15         }
16         printf("n = %d,m = %d,ans = %d\n", n, m, ans);
17     }
18 
19 
20 
21     return 0;
22 }
23 
24 int jiecheng(int n) {
25     int sum = 1;
26     if (n == 0) {
27         return 1;
28     }
29     else {
30         for (int t = 1; t <= n; t++) {
31             sum *= t;
32         
33         }
34         return sum;
35 
36     }
37 
38 
39 }

递归

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int jiecheng(int t);
 5 
 6 int main() {
 7     int ans;
 8     int n, m;
 9     while (scanf_s("%d%d", &n, &m) != EOF) {
10         if (m > n) {
11             ans = 0;
12         }
13         else {
14             ans = jiecheng(n) / (jiecheng(m) * jiecheng(n - m));
15         }
16         printf("n = %d,m = %d,ans = %d\n", n, m, ans);
17     }
18 
19 
20 
21     return 0;
22 }
23 
24 int jiecheng(int n) {
25     if (n == 0)
26         return 1;
27     else {
28         return n * jiecheng(n - 1);
29     
30     
31     }
32 
33 
34 }

 

任务七:

 1 void print_charman(int n);
 2 int main() {
 3     int n;
 4     printf("Enter n:");
 5     scanf_s("%d", &n);
 6     print_charman(n);
 7 
 8     return 0;
 9 
10 
11 }
12 
13 void print_charman(int n) {
14     int s = 0;
15     int k;
16     k = (n * 2) - 1;
17     for (int t = k; t > 0; t -= 2) {
18         for (int t = 0; t < s; t++) {
19             printf("\t");
20         }
21         for (int a = 0; a < t; a++) {
22             printf(" O \t");
23         }
24         printf("\n");
25         for (int t = 0; t < s; t++) {
26             printf("\t");
27         }
28         for (int a = 0; a < t; a++) {
29             printf("<H>\t");
30         }
31         printf("\n");
32         for (int t = 0; t < s; t++) {
33             printf("\t");
34         }
35         for (int a = 0; a < t; a++) {
36             printf("I I\t");
37         }
38         printf("\n");
39         printf("\n");
40         s++;
41 
42 
43 
44     }
45 
46 
47 
48 }

 

posted @ 2024-10-23 18:31  Little_Zcy  阅读(18)  评论(0编辑  收藏  举报