实验3

任务1

 1 #include<stdio.h>
 2 #include <stdio.h>
 3 char score_to_grade(int score); 
 4 int main() {
 5     int score;
 6     char grade;
 7     while(scanf("%d", &score) != EOF) {
 8         grade = score_to_grade(score); 
 9         printf("分数: %d, 等级: %c\n\n", score, grade);
10    }
11    return 0;
12 }
13 
14 char score_to_grade(int score) {
15     char ans;
16     switch(score/10) {
17     case 10:
18     case 9:  ans = 'A'; break;
19     case 8:  ans = 'B'; break;
20     case 7:  ans = 'C'; break;
21     case 6:  ans = 'D'; break;
22     default: ans = 'E';
23    }
24     return ans;
25 }

问题1:功能为将输入的分数转换为对应的等级。形参类型是int,返回值类型是char

问题2:没有使用break,这会使运行时不能跳出switch,而且不应该使用“ ”,这是字符串常量,char是单个字符,应该用‘ ’

 

 

任务2

#include <stdio.h>
int sum_digits(int n); 
int main() {
    int n;
    int ans;
    while(printf("Enter n: "), scanf("%d", &n) != EOF) {
        ans = sum_digits(n); 
        printf("n = %d, ans = %d\n\n", n, ans);
   }
    return 0;
}

int sum_digits(int n) {
    int ans = 0;
    while(n != 0) {
         ans += n % 10;
         n /= 10;
   }
    return ans;
}

问题1:作用是返回一个数的各个位的数字之和

问题2:能实现同等效果。原方法用的是迭代思想,运用循环实现。后一种是递归思维,是将问题不断分解为更小的问题,直到可以直接求解。

 

任务3

 1 #include <stdio.h>
 2 int power(int x, int n);    
 3 
 4 int main() {
 5     int x, n;
 6     int ans;
 7 
 8     while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) {
 9         ans = power(x, n);  
10         printf("n = %d, ans = %d\n\n", n, ans);
11     }
12     
13     return 0;
14 }
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:是递归函数。

 

 

任务4

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

 

任务5

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

 

 

 

任务6

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

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

 

 

 

 

任务7

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

 

posted @ 2024-10-24 02:39  202483290420王璐  阅读(15)  评论(0编辑  收藏  举报