实验三

任务一

 1 #include <stdio.h>
 2 char score_to_grade(int score);  // 函数声明
 3 
 4 int main() {
 5     int score;
 6     char grade;
 7 
 8     while(scanf("%d", &score) != EOF) {
 9         grade = score_to_grade(score);  // 函数调用
10         printf("分数: %d, 等级: %c\n\n", score, grade);
11     }
12 
13     return 0;
14 }
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.无break,switch语句将不会终止

任务二

 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("%d", &n) != EOF) {
 7         ans = sum_digits(n);    
 8         printf("n = %d, ans = %d\n\n", n, ans);
 9     }
10 
11     return 0;
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.求各位上数的和

2.能,前者迭代,后者递归

任务三

 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 int power(int x, int n) {
13     int t;
14     if(n == 0)
15         return 1;
16     else if(n % 2)
17         return x * power(x, n-1);
18     else {
19         t = power(x, n/2);
20         return t*t;
21     }
22 }

1.计算x的n次方

2.能

任务四

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

 任务五

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

任务六

 1 #include <stdio.h>
 2 int func(int n, int m);   // 函数声明
 3 int fac(int n); 
 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     int s;
17     s=fac(n)/fac(m)/fac(n-m);
18     if (n<m){
19         return 0;
20     }
21     else{
22         return s;
23     }
24 }
25 int fac(int n){
26     int i,s=1;
27     if(n==0){
28         return 1;
29     }
30     else{
31         for(i=1;i<=n;i++){
32             s=s*i;
33         }
34         return s;
35     }
36 }

 1 #include <stdio.h>
 2 int func(int n, int m);   // 函数声明
 3 int main() {
 4     int n, m;
 5     int ans;
 6 
 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 int func(int n,int m){
15     if(n==m||m==0){    
16     return 1;}
17     else if(n<m)
18     return 0;
19     else{
20         return func(n-1,m)+func(n-1,m-1);
21     }
22 }

任务七

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

 

posted @ 2024-10-29 23:54  马子浩  阅读(3)  评论(0编辑  收藏  举报