Yh-c-learning

导航

 

实验任务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 
30     return ans;
31 }
View Code

 

 

 

运行截图:

屏幕截图 2026-04-19 174149

问题1:score_to_grade函数实现了将分数转换为等级的操作 形参为整形score 返回值为字符型
问题2:case语段后缺少break,并且错误使用了“”,使ans无法被赋予数值,会报错,应该使用‘’才合理

 

实验任务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 }
View Code

 

运行截图:

屏幕截图 2026-04-21 142310

问题1:sum_digits的作用是把这个数每个位上的数字相加
问题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 }
View Code

运行截图:

屏幕截图 2026-04-21 143130

问题一:power函数实现幂的运算,计算并返回x的n次幂
问题二:是

IMG20260421144724

 

实验任务4

源代码:

 1 #include <stdio.h>
 2 
 3 
 4 int classify_triangle(int a, int b, int c);
 5 
 6 int main() {
 7     int a, b, c;
 8     int res;
 9    
10     while (printf("Enter a b c: "), scanf("%d%d%d", &a, &b, &c) != EOF) {
11         res = classify_triangle(a, b, c);
12         
13         switch (res) {
14             case 0:
15                 printf("不能构成三角形\n");
16                 break;
17             case 1:
18                 printf("普通三角形\n");
19                 break;
20             case 2:
21                 printf("等边三角形\n");
22                 break;
23             case 3:
24                 printf("等腰三角形\n");
25                 break;
26             case 4:
27                 printf("直角三角形\n");
28                 break;
29             default:
30                 printf("输入无效\n");
31         }
32     }
33     return 0;
34 }
35 
36 
37 int classify_triangle(int a, int b, int c) {
38     
39     int max_side;
40     int is_right;
41 
42    
43     if (a <= 0 || b <= 0 || c <= 0) {
44         return 0;
45     }
46     if (a + b <= c || a + c <= b || b + c <= a) {
47         return 0;
48     }
49 
50     
51     if (a == b && b == c) {
52         return 2;
53     }
54 
55     
56     max_side = a;
57     if (b > max_side) {
58         max_side = b;
59     }
60     if (c > max_side) {
61         max_side = c;
62     }
63 
64     is_right = 0;
65     if (max_side == a) {
66         is_right = (a * a == b * b + c * c);
67     } else if (max_side == b) {
68         is_right = (b * b == a * a + c * c);
69     } else {
70         is_right = (c * c == a * a + b * b);
71     }
72 
73     if (is_right) {
74         return 4;
75     }
76 
77   
78     if (a == b || a == c || b == c) {
79         return 3;
80     }
81 
82     
83     return 1;
84 }
View Code

运行截图

屏幕截图 2026-04-21 150630

 

实验任务5

源代码(迭代):

 

 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("%d%d", &n, &m) != EOF) {
 9         ans = func(n, m);
10         printf("n = %d, m = %d, ans = %d\n", n, m, ans);
11     }
12     return 0;
13 }
14 
15 
16 int func(int n, int m) {
17 
18     long long res;
19     int i;
20 
21    
22     if (m < 0 || m > n) {
23         return 0;
24     }
25     if (m == 0 || m == n) {
26         return 1;
27     }
28 
29     
30     if (m > n - m) {
31         m = n - m;
32     }
33 
34    
35     res = 1; 
36     for (i = 1; i <= m; i++) { 
37         res = res * (n - m + i) / i;
38     }
39 
40     return (int)res;
41 }
View Code

 

运行截图

屏幕截图 2026-04-21 151603

 

 

源代码(递归):

 

 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 int func(int n,int m){
16     if(m == 0)
17     return 1;
18     else if(m == n)
19     return 1;
20     else if(m > n)
21     return 0;
22     else 
23     return func(n-1,m)+func(n-1,m-1);
24 }
View Code

运行截图

屏幕截图 2026-04-21 223614

 

实验任务6

源代码:

 1 #include <stdio.h>
 2 
 3 
 4 int gcd(int a, int b, int c);
 5 
 6 int main() {
 7     
 8     int a, b, c;
 9     int ans;
10 
11   
12     while (scanf("%d%d%d", &a, &b, &c) != EOF) {
13         ans = gcd(a, b, c);   
14         printf("最大公约数:%d\n", ans);
15     }
16 
17     return 0;
18 }
19 
20 
21 int gcd(int a, int b, int c) {
22    
23     int min_val;
24     int i;
25 
26    
27     min_val = a;
28     if (b < min_val) min_val = b;
29     if (c < min_val) min_val = c;
30 
31     
32     for (i = min_val; i >= 1; i--) {
33         if (a % i == 0 && b % i == 0 && c % i == 0) {
34             return i; 
35         }
36     }
37 
38     
39     return 1;
40 }
View Code

运行截图

屏幕截图 2026-04-21 225247

 

实验任务7

源代码:

 1 #include <stdio.h>
 2 
 3 
 4 void print_charman(int n);
 5 
 6 int main() {
 7     int n;
 8     printf("input n: ");
 9     scanf("%d", &n);
10     print_charman(n);
11     return 0;
12 }
13 
14 
15 void print_charman(int n) {
16     int i,j ;
17     for ( i = 0; i < n; i++) {
18         int cnt = 2 * n - 1 - 2 * i;
19         int fs = ((2 * n - 1) - cnt) * 4 / 2;
20 
21         for ( j = 0; j < fs; j++) {
22             printf(" ");
23         }
24 
25         for ( j = 0; j < cnt; j++) {
26             printf(" O  ");
27         }
28         printf("\n");
29 
30 
31         for ( j = 0; j < fs; j++) {
32             printf(" ");
33         }
34 
35         for ( j = 0; j < cnt; j++) {
36             printf("<H> ");
37         }
38         printf("\n");
39         for ( j = 0; j < fs; j++) {
40             printf(" ");
41         }
42         for ( j = 0; j < cnt; j++) {
43             printf("I I ");
44         }
45         printf("\n");
46     }
47 }
View Code

运行截图

屏幕截图 2026-04-21 230558

屏幕截图 2026-04-21 230610

 

posted on 2026-04-21 23:08  葛益豪  阅读(10)  评论(0)    收藏  举报