实验3

实验1

 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 {
13     int line, col, i;
14     char text[N] = "hi,November~"; 
15     
16     srand(time(0));//以当前系统时间作为随机种子
17     
18     for(i = 1;i<=10;++i){
19         line = rand() % 25;
20         col = rand() % 80;
21         print_text(line, col, text);
22         Sleep(1000);//暂停1000ms 
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     for(i = 1;i<=n;++i)
39     printf("\n");
40 } 
41 
42 //在第line行第col列打印一段文本
43 void print_text(int line, int col, char text[]){
44     print_blank_lines(line-1);    //打印line-1行空行 
45     print_spaces(col-1);          //打印col-1列空格 
46     printf("%s",text);            //在第line行col列输出text中字符串 
47 } 

 

实验2.1

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

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

实验3

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

 

实验4

 1 #include<stdio,h>
 2 int func(int n , int m);
 3 
 4 int main()
 5 {
 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     }
11     return 0;
12 }
13 
14 int func(int n, int m){
15     if(m==0||m==n)
16     return 1;
17     
18     return func(n-1,m) + func(n-1,m-1);
19 }

 

5

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

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

 

posted @ 2023-10-30 17:01  Gustave-Ehsy  阅读(11)  评论(0)    收藏  举报