實驗3 c語言函式應用程式設計

施5114發表於2024-10-28

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_s("%d",&score) != EOF) {
10         grade = score_to_grade(score);
11         printf("分數:%d,等級:%c\n\n", score, grade);
12 
13     }
14 
15     return 0;
16 
17 }
18 
19 char score_to_grade(int score) {
20     char ans;
21 
22     switch (score / 10) {
23     case 10:
24     case 9:   ans = 'A';break;
25     case 8:   ans = 'B';break;
26     case 7:   ans = 'C';break;
27     case 6:   ans = 'D';break;
28     default:  ans = 'E';
29 
30     }
31     return ans;
32      
33 }

問題1:功能是將鍵盤輸入的分數轉化成對應的等級 形參型別是整型 返回值型別是字元型

問題2:1 改成雙引號後無法輸出對應的等級 2 語句後不加break,程式執行到對應語句後無法終止,還會輸出後面語句的結果

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

問題1:功能是得出輸入數各位的加和

問題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_s("%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         else if (n % 2)
23             return x * power(x, n - 1);
24         else {
25             t = power(x, n / 2);
26             return t * t;
27 
28         }
29 
30     }

問題1:功能是計算x的n次方

問題2:是遞迴函式

task4:

 1 #include<stdio.h>
 2 #include<math.h>
 3 int is_prime(int n);
 4 
 5 
 6 int main() {
 7     int n,sum;
 8     n = 1;
 9     sum = 0;
10     printf("100以內的孿生素數:\n");
11     for (n = 1;n <= 98;++n) {
12         if (is_prime(n)&& is_prime(n + 2)) {
13             printf("%d %d\n", n, n + 2);
14             sum = sum + 1;
15         }
16     }
17     printf("100以內的孿生素數共有%d個", sum);
18 
19     return 0;
20         
21     
22     }
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 int is_prime(int n) {
33     int i, m;
34     m = sqrt(n);
35 
36     for (i = 2;i<=m;++i)
37         if (n % i == 0)
38             break;
39     if (i > m&&n>1)
40         return 1;
41     else
42         return 0;
43 }
44     

task5:

 1 #include<stdio.h>
 2 void hanoi(int n, char from, char temp, char to);
 3 void moveplate(int n, char from, char to);
 4 auto sum = 0;
 5 int main() {
 6     int n;
 7     while (scanf_s("%d", &n)!=EOF) {
 8         hanoi(n, 'A', 'B', 'C');
 9         printf("一共移動了%d次\n\n", sum);
10         sum = 0;
11     }
12     return 0;
13 }
14 
15 void hanoi(int n, char from, char temp, char to) {
16     if (n == 1) {
17         moveplate(n, from, to);
18     }
19 
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(int n, char from, char to){
27     printf("%d:%c-->%c\n", n, from, to);
28     sum += 1;
29 }

task6:

 1 #include<stdio.h>
 2 int func(int n, int m);
 3 
 4     int main(){
 5         int n, m;
 6         int ans;
 7 
 8         while (scanf_s("%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 
16     int func(int n, int m) {
17         int x=1, y=1;
18         int i, j;
19         for (i = n;i >= (n - m + 1);--i)
20             x = x * i;
21         for (j = 1;j <= m;++j)
22             y = y * j;
23         return x / y;
24         
25 
26 
27 
28 
29     }

 1 #include<stdio.h>
 2 int func(int n, int m);
 3 
 4     int main(){
 5         int n, m;
 6         int ans;
 7 
 8         while (scanf_s("%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 
16     int func(int n, int m) {
17         if (m == 0 || n == m )
18             return 1;
19         else if(m>n)
20             return 0;
21         else {
22             return func(n - 1, m) + func(n - 1, m - 1);
23         }
24             

task7:

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

相關文章