实验3

任务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     }
11     return 0;
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: 函数score_to_grade功能:按照分数范围给分数打上等级

问题2:若无break,则只能在成绩为90-100分时输出ABCDE,其他不输出

 

任务2

 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     return 0;
11 }
12 int sum_digits(int n){
13     int ans = 0;
14     while(n!=0){
15         ans+=n%10;
16         n/=10;
17         
18     }
19     return ans;
20 }

 

 

问题1:求数字所有位上的数字之和

问题2:能实现同等效果,原代码为循环结构,改后为判断结构

任务3

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

 问题1:函数power的作用为计算x的n次幂

问题2:是递归函数   n=0时x^n=0,n>0时x^n=x*x^(n-1)

任务4

 

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

任务5

 

 

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

 任务6

 1 #include<stdio.h>
 2 int func(int n, int m);
 3 int main()
 4 {
 5     int n, m;
 6     int ans;
 7     while (scanf_s("%d%d", &n, &m) != EOF)
 8     {
 9         ans = func(n, m);
10         printf("n = %d,m = %d,ans = %d \n\n", n, m, ans);
11 
12     }
13 
14     return 0;
15 }
16 int func(int n, int m)
17 {
18     if (m == 0 || m == n)
19         return 1;
20     else if (n < m)
21         return 0;
22     else
23         return func(n - 1, m) + func(n - 1, m - 1);
24 }

 任务7

 

 

#include <stdio.h>
void print_charman(int n);
int main()
{
    int n;
    printf("Enter n: ");
    scanf_s("%d", &n);
    print_charman(n); 
    return 0;
}
void print_charman(int n)
{
    int i;
    int t = 0;
    for ( i = n; i >= 1; i--)
    {
        for (int j = 0; j < t; j++)
            printf("\t");
        for (int j = 0; j < 2 * i - 1; j++)
            printf(" O\t");
        printf("\n");
        for (int j = 0; j < t; j++)
            printf("\t");
        for (int j = 0; j < 2 * i - 1; j++)
            printf("<H>\t");
        printf("\n");
        for (int j = 0; j < t; j++)
            printf("\t");
        for (int j = 0; j < 2 * i - 1; j++)
            printf("I I\t");
        printf("\n");
        t++;
    }
}

 

posted @ 2024-10-30 13:00  palpitate-li  阅读(3)  评论(0编辑  收藏  举报