实验3

任务1: 源代码:
 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 // 函数定义
18 char score_to_grade(int score) {
19     char ans;
20 
21     switch(score/10) {
22     case 10:
23     case 9:   ans = 'A'; 
24     case 8:   ans = 'B'; 
25     case 7:   ans = 'C'; 
26     case 6:   ans = 'D'; 
27     default:  ans = 'E';
28     }
29 
30     return ans;
31 }

问题1:检测输入的分数值并返回对应等级,形参实参类型均为字符型

问题2:缺少break将代码终止,会使程序持续赋值直到将ans赋为e

 

任务2:

源代码:

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

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

问题1:计算x的n次方

问题2:是递归函数。

 

 

任务4:

源代码:

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

 

 

任务5:

源代码:

 

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

 

 

任务6:

源代码:

#include <stdio.h>
int func(int n, int m); // 函数声明
int main() {
    int n, m;
    int ans;
    while (scanf("%d%d", &n, &m) != EOF) {
        ans = func(n, m); // 函数调用
        printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
    }
    return 0;
}

int func(int n, int m) {
    if (m > n || m < 0) {
        return 0;
    }
    if (m == 0 || m == n) {
        return 1;
    }
    int ans = 1;
    for (int i = 1; i <= m; i++) {
        ans *= (n - i + 1);
        ans /= i;
    }
    return ans;
}
#include <stdio.h>
int func(int n, int m); // 函数声明
int main() {
    int n, m;
    int ans;
    while (scanf("%d%d", &n, &m) != EOF) {
        ans = func(n, m); // 函数调用
        printf("n = %d, m = %d, ans = %d\n\n", n, m, ans);
    }
    return 0;
}

int func(int n, int m) {
    if (m > n || m < 0) {
        return 0;
    }
    if (m == 0 || m == n) {
        return 1;
    }
    return func(n - 1, m - 1) + func(n - 1, m);
}

 

 

任务7:

源代码:

#include <stdio.h>
#include <stdlib.h>

char print_charman(int n);
int main() {
    int n;

    printf("Enter n: ");
    scanf("%d", &n);
    print_charman(n); // 函数调用

    return 0;
}

char print_charman(int n)
{
    int p, m, t;
    for (p = n; p > 0; --p) {
        for (t = 0; t < n - p; ++t)
        {
            printf("\t");
        }
        for (m = 2 * p - 1; m > 0; --m)
        {
            printf(" O \t");

        }
        printf("\n");
        for (t = 0; t < n - p; ++t)
        {
            printf("\t");
        }
        for (m = 2 * p - 1; m > 0; --m)
        {
            printf("<H>\t");

        }
        printf("\n");
        for (t = 0; t < n - p; ++t)
        {
            printf("\t");
        }
        for (m = 2 * p - 1; m > 0; --m)
        {
            printf("I I\t");

        }
        printf("\n");

    }
}

 

 

posted @ 2024-10-30 18:52  jianglan233  阅读(3)  评论(0编辑  收藏  举报