实验任务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 }

 

运行截图

实验任务2

源代码

 1 #include <stdio.h>
 2 long long fac(int n); 
 3 int main() {
 4 int i, n;
 5 printf("Enter n: ");
 6 scanf("%d", &n);
 7 for (i = 1; i <= n; ++i)
 8 printf("%d! = %lld\n", i, fac(i));
 9 return 0;
10 }
11 
12 long long fac(int n) {
13 static long long p = 1;
14 p = p * n;
15 return p;
16 }

 

运行截图

 实验任务2——2

源代码

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

 

运行截图

 实验任务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 long long func(int n)
13 {
14     if(n==1)
15     return 1;
16     else 
17     return 2*func(n-1)+1;
18     
19 }

 

运行截图

 实验任务4

源代码

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

 

运行截图

 实验任务5

源代码

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

 

运行截图

 

实验任务6

源代码

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

 

运行截图

 

posted on 2023-11-06 00:58  wujinyu  阅读(7)  评论(0编辑  收藏  举报