實驗3

AGHAG發表於2024-11-10

task1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 
 7 void print_text(int line, int col, char text[]);  // 函式宣告 
 8 void print_spaces(int n);  // 函式宣告 
 9 void print_blank_lines(int n); // 函式宣告 
10 
11 int main() {
12     int line, col, i;
13     char text[N] = "hi, April~";
14     
15     srand(time(0)); // 以當前系統時間作為隨機種子
16     
17     for(i = 1; i <= 10; ++i) {
18         line = rand() % 25;
19         col =  rand() % 80;
20         print_text(line, col, text);
21         Sleep(1000);  // 暫停1000ms
22     }
23     
24     return 0; 
25 }
26 
27 // 列印n個空格 
28 void print_spaces(int n) {
29     int i;
30     
31     for(i = 1; i <= n; ++i)
32         printf(" ");
33 }
34 
35 // 列印n行空白行
36 void print_blank_lines(int n) {
37     int i;
38     
39     for(i = 1; i <= n; ++i)
40         printf("\n");
41  } 
42 
43 // 在第line行第col列列印一段文字 
44 void print_text(int line, int col, char text[]) {
45     print_blank_lines(line-1);      // 列印(line-1)行空行 
46     print_spaces(col-1);            // 列印(col-1)列空格
47     printf("%s", text);         // 在第line行、col列輸出text中字串
48 }

程式每隔一秒列印hi,April~,27、35行下面的函式分別控制空格數和空行數

task2

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函式宣告
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13     system("pause");
14     return 0;
15     
16 }
17 
18 // 函式定義
19 long long fac(int n) {
20     static long long p = 1;
21     p = p * n;
22 
23     return p;
24     
25 }

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n); // 函式宣告
 4 
 5 int main() {
 6     int i, n;
 7 
 8     printf("Enter n: ");
 9     scanf("%d", &n);
10 
11     for (i = 1; i <= n; ++i)
12         printf("%d! = %lld\n", i, fac(i));
13     system("pause");
14     return 0;
15     
16 }
17 
18 // 函式定義
19 long long fac(int n) {
20     static long long p = 1;
21     printf("p=%11d\n",p);
22     p = p * n;
23 
24     return p;
25     
26 }

即使函式執行完畢後,區域性static變數值也會儲存下來

task3

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 long long func(int n); // 函式宣告
 4 
 5 int main() {
 6     int n;
 7     long long f;
 8 
 9     while (scanf("%d", &n) != EOF) {
10         f = func(n); // 函式呼叫
11         printf("n = %d, f = %lld\n", n, f);
12     }
13 
14     system("pause");
15     return 0;
16 }
17 long long func(int n)
18 {
19     if(n==0)
20     {
21         return 0;
22     }
23     else
24     {
25         return 2*func(n-1)+1;
26     }
27 }

task4

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int func(int n, int m);
 4 
 5 int main() {
 6     int n, m;
 7 
 8     while(scanf("%d%d", &n, &m) != EOF)
 9         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
10     system("pause");
11     return 0;
12 }
13 
14 // 函式定義
15 int func(int n,int m)
16 {
17     int s=1;
18     int i;
19     int k=1;
20     if(m>n)    
21     {
22         return 0;
23     }
24     else if(m==0||m==n)
25     {
26         return 1;
27     }
28     else
29     {
30           for(i=1;i<=m;++i)
31       {
32 
33           s=s*(n-i+1);
34          k*=i;
35       }
36           
37           return s/k;
38     }
39     
40 }

遞迴

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

task5

 1 #include <stdio.h>
 2 
 3 
 4 int hanoi(int n, char from, char to, char temp) {
 5     if (n == 0) {
 6         return 0; 
 7     }
 8     int moves1 = hanoi(n - 1, from, temp, to);
 9     printf("%d : %c --> %c\n", n, from, to);
10     moves1++; 
11     int moves2 = hanoi(n - 1, temp, to, from);
12     return moves1 + moves2;
13 }
14 
15 int main() {
16     int n, move;
17     printf("Enter the number of disks: ");
18     while(scanf("%d", &n) != EOF)
19     {
20     move= hanoi(n, 'A', 'C', 'B'); 
21     printf("一共移動了%d次\n", move); 
22     }
23     return 0;
24 }

task6

#include <stdio.h>
#include <math.h>
#include<stdlib.h>
long func(long s);   // 函式宣告

int main()
{

    long s, t;

    printf("Enter a number: ");
    while (scanf("%ld", &s) != EOF) {
        t = func(s); // 函式呼叫
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
    system("pause");
    return 0;
}

// 函式定義
long func(long s)
{
    long ans;
    long t,k;
    ans=0;
    t=1;
    while(s!=0)
    {
        k=s%10;
        if(k%2!=0)
        {
            ans+=t*k;
            t*=10;
        }
        s/=10;
    }
    return ans;
}

相關文章