实验三

任务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:作为一个函数,根据分数划分等级。形参为整数型score,返回值为字符型ans。

问题2:有,少了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:能,原函数为迭代,分别算出每位的数并加起来,改函数为递归,通过调用自身来求出结果

任务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:是, 偶数先计算x的n/2次幂,再将结果平方。奇数减一转为偶数,计算n-1次幂,然后将结果乘以x

 

任务4:

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int is_prime(int n);
 5 
 6 int main()
 7 {
 8     int n;
 9     int cnt = 0;
10     printf("100以内的孪生素数:\n");
11     for (n = 1; n <= 98; n++)
12     {
13         if (is_prime(n) && is_prime(n + 2))
14         {
15             printf("%d %d\n", n, n + 2);
16             cnt++;
17         }
18     }
19     printf("100以内的孪生素数共有%d个\n", cnt);
20     return 0;
21 }
22 
23 int is_prime(int n)
24 {
25     int i;
26     if (n == 1)
27         return 0;
28     if (n == 2)
29     {
30         return 1;
31     }
32     if (n % 2 == 0)
33         return 0;
34     for (i = 3; i <= sqrt(n); i += 2)
35     {
36         if (n % i == 0)
37         {
38             return 0;
39         }
40     }
41     return 1;
42 }

任务5:

 

 1 #include <stdio.h>
 2 
 3 int cnt = 0;
 4 
 5 void print(unsigned int n, char from, char to)
 6 {
 7     cnt++;
 8     printf("%u : %c-->%c\n", n, from, to);
 9 }
10 void hanoi(unsigned int n, char A, char B, char C)
11 {
12     if (n == 1)
13     {
14         print(n, A, C);
15     }
16     else
17     {
18         hanoi(n - 1, A, C, B);
19         print(n, A, C);
20         hanoi(n - 1, B, A, C);
21     }
22 }
23 int main()
24 {
25     unsigned int n;
26     while (scanf("%u", &n) != EOF)
27     {
28         hanoi(n, 'A', 'B', 'C');
29         printf("一共移动了%d次\n", cnt);
30         cnt = 0;
31     }
32     return 0;
33 }

 

任务6:

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

 

 1 #include <stdio.h>
 2 int func(int n, int m);
 3 int main()
 4 {
 5     int n, m;
 6     int ans;
 7     while (scanf("%d%d", &n, &m) != EOF)
 8     {
 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 {
18     if (m == 0 || n == m)
19         return 1;
20     else
21         return func(n - 1, m) + func(n - 1, m - 1);
22 }

 任务7:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 static int a = 0, b = 0, c = 0;
 5 void print_charman();
 6 
 7 int main()
 8 {
 9     int n;
10 
11     printf("Enter n:");
12     scanf("%d", &n);
13     print_charman(n);
14 
15     return 0;
16 }
17 
18 void print_charman(int n)
19 {
20     int i;
21     if (n == 0)
22     {
23         return;
24     }
25     for (i = 0; i < a; i++)
26     {
27         printf("   ");
28         printf("\t");
29     }
30     a++;
31     for (i = 0; i < 2 * n - 1; i++)
32     {
33         printf(" O ");
34         printf("\t");
35     }
36     printf("\n");
37     for (i = 0; i < b; i++)
38     {
39         printf("   ");
40         printf("\t");
41     }
42     b++;
43     for (i = 0; i < 2 * n - 1; i++)
44     {
45         printf("<H>");
46         printf("\t");
47     }
48     printf("\n");
49     for (i = 0; i < c; i++)
50     {
51         printf("   ");
52         printf("\t");
53     }
54     c++;
55     for (i = 0; i < 2 * n - 1; i++)
56     {
57         printf("I I");
58         printf("\t");
59     }
60     printf("\n");
61     print_charman(n - 1);
62 }

 

posted @ 2024-10-24 21:59  Erhjiu  阅读(13)  评论(0编辑  收藏  举报