实验三

实验1

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

问题1

功能:将分数转化为等级    形参整型   返回值字符型

问题2

swich语句case后加break跳出分支

实验2

 1 #include <stdio.h>
 2 
 3 intsum_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     
15     return 0;
16     }
17     
18     int sum_digits(){
19         int ans = 0;
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     
15     return 0;
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     }
30 }

 问题1

功能 计算 x 的n次方

 

 

 

 

实验4

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

 

实验5

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

实验6

迭代

 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     int i, ch1= 1, ch2 = 1 ; 
15     for (i = 1;i<=m;i++)
16     ch1 *= i; 
17     for (i = n - m + 1;i<= n;i++)
18     ch2 *= i;
19     return ch2/ch1;  
20     
21 }

递归

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

 

实验7

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 void print_charman(int n);
 4 int k = 0;
 5 int main() {
 6 int n;
 7 printf("Enter n: ");
 8 scanf("%d", &n);
 9 print_charman(n); // 函数调用
10 return 0;
11 }
12 
13 void print_charman(int n){
14     if (n <= 0) {
15         return;  // 递归终止条件
16     }
17     int i,j;
18         for(i = 0;i<k;i++)
19         printf("\t");
20         for (j = 0;j<2*n-1;j++){
21             printf(" O      ");
22         }
23         printf("\n");
24         
25         for(i = 0;i<k;i++)
26         printf("\t");
27         for(j = 0;j<n*2-1;j++){
28             printf("<H>     ");
29         }
30         printf("\n");
31         
32         for(i = 0;i<k;i++)
33         printf("\t");
34         for(j = 0;j<n*2-1;j++){
35             printf("I I     ");
36         }
37         k++;
38         printf("\n\n");
39         
40     print_charman(n-1);
41     
42 }

 

posted @ 2024-10-25 20:09  朴飞  阅读(7)  评论(0编辑  收藏  举报