实验3

task1

源代码

 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:定义一个函数,字符型,字母

问题2:有问题,程序会一直进行下去

task2

源代码

 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 }

运行结果

问题一:定义一个新函数

问题二:能,改为非递归函数也可以

task3

源代码

 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:输出一个数的幂和他的次方值

问题2:如果n=0,返回一,如果n是偶数,则运行程序

task4

源代码

 1 #include <stdio.h>
 2 
 3 int is_prime(int n) {
 4     if (n <= 1) {
 5         return 0;
 6     }
 7     for (int i = 2; i * i <= n; i++) {
 8         if (n % i == 0) {
 9             return 0;
10         }
11     }
12     return 1;
13 }
14 
15 int main() {
16     int count = 0; 
17 
18     printf("100以内的孪生素数有:\n");
19     for (int n = 1; n <= 98; n++) {
20         if (is_prime(n) && is_prime(n + 2)) {
21             printf("(%d, %d)\n", n, n + 2);
22             count++;
23         }
24     }
25 
26     printf("孪生素数的总数为:%d\n", count);
27 
28     return 0;
29 }

运行结果

task5

源代码

 1 #include <stdio.h>
 2 
 3 void hanoi(int n, char from, char to, char aux, int* moveCount) {
 4     if (n == 1) {
 5         printf(" 1 from %c --> %c\n", from, to);
 6         (*moveCount)++;
 7     } else {
 8         hanoi(n - 1, from, aux, to, moveCount);
 9         printf(" %d from %c --> %c\n", n, from, to);
10         (*moveCount)++;
11         hanoi(n - 1, aux, to, from, moveCount);
12     }
13 }
14 
15 int main() {
16     int  n;
17 
18     for (int i = 0; i < 100; i++) {
19         printf("Enter the number of disks for case %d: ", i + 1);
20         scanf("%d", &n);
21 
22         int moveCount = 0;
23 
24         printf("Movement plan for %d disks:\n", n);
25         hanoi(n, 'A', 'C', 'B', &moveCount);
26 
27         printf("一共移动了: %d\n\n", moveCount);
28     }
29 
30     return 0;
31 }

运行结果

task6

源代码 迭代

 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(n<m) {
15         return 0;
16     }
17     int C[n + 1][m + 1];
18     
19     for (int i = 0; i <= n; i++) {
20         for (int j = 0; j <= (i < m ? i : m); j++) {
21             if (j == 0 || j == i) {
22                 C[i][j] = 1;
23             } else {
24                 C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
25             }
26         }
27     }
28     
29     return C[n][m];
30 }

运行结果

源代码递归

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

运行结果

task7

源代码

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

运行结果

 

posted @ 2024-10-29 16:52  yuanjialiang  阅读(3)  评论(0编辑  收藏  举报