实验3

任务1

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 void print_text(int line, int col, char text[]); 
 7 void print_spaces(int n); 
 8 void print_blank_lines(int n); 
 9 int main() {
10     int line, col, i;
11     char text[N] = "hi, November~";
12     srand(time(0));
13         for(i = 1; i <= 10; ++i) {
14             line = rand() % 25;
15             col = rand() % 80;
16             print_text(line, col, text);
17             Sleep(1000); 
18             }
19     return 0;
20         }
21 
22     void print_spaces(int n) {
23         int i;
24             for(i = 1; i <= n; ++i)
25                 printf(" ");
26         }
27 
28     void print_blank_lines(int n) {
29         int i;
30             for(i = 1; i <= n; ++i)
31                 printf("\n");
32         }
33 
34     void print_text(int line, int col, char text[]) {
35         print_blank_lines(line-1); 
36         print_spaces(col-1); 
37         printf("%s", text); 
38 }

运行结果

 

 

代码作用:随机在屏幕上的某个位置输出 hi,+当前月份November,生成十个,每隔1秒生成一个

 

 

任务2

 2.1

代码

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

运行结果

 

2.2

代码

 1 #include <stdio.h>
 2 int func(int, int);    
 3 
 4 int main() {
 5     int k = 4, m = 1, p1, p2;
 6 
 7     p1 = func(k, m);    
 8     p2 = func(k, m);    
 9     printf("%d, %d\n", p1, p2);
10 
11     return 0;
12 }
13 
14 int func(int a, int b) {
15     static int m = 0, i = 2;
16 
17     i += m + 1;
18     m = i + a + b;
19 
20     return m;
21 }

运行结果

 static变量作用:使得i与m的值仅被赋值一次,保留其运算后的值再进行下一次运算

 

 

任务3

代码

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

 

 

运行截图

 

 

任务4

 实现方式1

代码

 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 int func(int n,int m)
14 {
15     int ret = 1;
16     int i;
17     for (i = 1; i <= n; i++)
18         ret = ret * (m - i + 1) / i;
19     return ret;
20 }

截图

 

 

实现方式2

 代码

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

结果

 

 

 

 

 

任务5

代码

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

结果

 

 

 

任务6

代码

 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s); // 函数声明
 4 int main() {
 5     long s, t;
 6     printf("Enter a number: ");
 7     
 8     while (scanf("%ld", &s) != EOF) {
 9         t = func(s); 
10         printf("new number is: %ld\n\n", t);
11         printf("Enter a number: ");
12     }
13     return 0;
14 }
15 
16 long func(long s) {
17     long n = 0;
18     long p = 1;
19 
20     while (s > 0) {
21         long d = s % 10;
22         if (d % 2 == 1) {
23             n += d * p;
24             p *= 10;
25         }
26         s /= 10;
27     }
28 
29     return n;
30 }

运行截图

 

posted @ 2023-11-05 15:53  starming  阅读(6)  评论(0编辑  收藏  举报