实验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 char score_to_grade(int score) {
17     char ans;
18 
19     switch(score/10) {
20     case 10:
21     case 9:   ans = 'A'; break;
22     case 8:   ans = 'B'; break;
23     case 7:   ans = 'C'; break;
24     case 6:   ans = 'D'; break;
25     default:  ans = 'E';
26     }

 

截图:

问题1:功能是将分数转化为评级,形参为整型,返回值为字符型

问题2:有,在处理完数据后不会跳出switch语句,继续判断下一个case

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 int sum_digits(int n) {
17     int ans = 0;
18 
19     while(n != 0) {
20         ans += n % 10;
21         n /= 10;
22     }
23 
24     return ans;
25 }

截图:

问题1:功能是实现每位上的数字相加

问题2:可以,法2使用了递归的思想

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 int power(int x, int n) {
17     int t;
18 
19     if(n == 0)
20         return 1;
21     else if(n % 2)
22         return x * power(x, n-1);
23     else {
24         t = power(x, n/2);
25         return t*t;
26     }
27 }

截图:

问题1:实现x的n次方的计算

问题2:是递归函数, 指数如果是奇数则为x**n*(x**n-1),如果是偶数则为x**(n/2)*x**(n/2)

task4源代码:

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

截图:

实验5源代码:

 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 s = 0;
 6 int main()
 7 {
 8     unsigned int n;
 9     while(scanf("%u",&n) != EOF){
10         s=0;
11         hanoi(n,'A','B','C');
12         printf("一共移动了%d次\n",s);
13     }
14     system("pause");
15     return 0;
16 }
17 void hanoi(unsigned int n,char from,char temp,char to){
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     printf("%u:%c-->%c\n",n,from,to);
28     s = s + 1;
29 }

截图:

task6源代码1:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int i,h,n,m,up=1,down=1,ans;
 5     while(scanf("%d%d",&n,&m) != EOF){
 6         if(m == 0)
 7         ans = 1;
 8         else{    
 9         for(i=n;i >= n-m+1;i--){
10             up = up * i;
11         }
12         for(h=m;h >= 1;h--){
13             down = down * h;
14         }
15         ans = up/down;
16         }
17         printf("n = %d,m = %d,ans = %d\n",n,m,ans);
18         }
19     return 0;
20 }

源代码2:

 1 #include <stdio.h>
 2 int func(int n,int m);
 3 int power(int n);
 4 int main(){
 5     int n,m,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     if(m>n)
14         return 0;
15     else if(n==m||m==0)
16         return 1;
17     else
18         return func(n-1,m)+func(n-1,m-1);
19 }

截图:

task7源代码:

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

截图:

 

posted @ 2024-10-26 11:08  有点火热  阅读(11)  评论(0编辑  收藏  举报