实验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     int line, col, i ;
13     char text[N]="hi,November~";
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);
22     }
23     return 0;
24 }
25 void print_spaces(int n){
26     int i;
27     
28     for(i=1;i<=n;++i)
29         printf(" ");
30         
31 }
32 
33 void print_blank_lines(int n){
34     int i;
35     
36     for(i=1;i<=n;++i)
37         printf("\n");
38 }
39 
40 void print_text(int line,int col,char text[]){
41     print_blank_lines(line-1);
42     print_spaces(col-1);
43     printf("%s",text);
44 }

截图

 实验2

2.1.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     for (i = 1; i <= n; ++i)
10         printf("%d! = %lld\n", i, fac(i));
11         
12         return 0;
13         
14 }
15 long long fac(int n){
16     static long long p = 1;
17     
18     p = p * n;
19     
20     
21     return p;
22 }

截图

 代码2.1.2

 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     for (i = 1; i <= n; ++i)
10         printf("%d! = %lld\n", i, fac(i));
11         
12         return 0;
13         
14 }
15 long long fac(int n){
16     static long long p = 1;
17     
18     p = p * n;
19     printf("p = %lld\n", p);
20     
21     return p;
22 }

截图

 代码2.2

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

截图

实验3

代码

 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     {
11         f = func(n);
12         printf("n = %d, f = %lld\n", n, f);
13     }
14 
15     system("pause");
16     return 0;
17 }
18 
19 
20 long long func(int n)
21 {
22     long long f = 1;
23 
24     if (n == 0)
25         f = 0;
26     else
27         f = func(n-1)*2+1;
28 
29     return f;
30 }

 

截图

 实验4

代码

 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 i,x,c,y,l;
16     for(i=n-m+1,x=1;i<=n;++i)
17         x=x*i;
18    
19     for(l=1,y=1;l<=m;++l)
20         y=y*l;
21     c=x/y;
22     return c;
23 }

 

 

截图

 

 实验5

代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void ta(int n, char from, char temp, char to);
 5 void move(int n, char from, char to);
 6 
 7 static int count = 0;
 8 
 9 int main()
10 {
11     int n;
12 
13     while (scanf("%d",&n) != EOF)
14     {
15         ta(n, 'A', 'B', 'C');
16         printf("Ò»¹²Òƶ¯ÁË%d´Î\n",count);
17         count = 0;
18     }
19 
20     system("pause");
21     return 0;
22 }
23 
24 
25 void ta(int n, char from, char temp, char to)
26 {
27     if( n == 1 )
28     {
29         move(n, from, to);
30     }
31     else
32     {
33         ta(n-1, from, to, temp);
34         move(n, from, to);
35         ta(n-1, temp, from, to);
36     }
37 }
38 
39 void move(int n, char from, char to)
40 {
41     printf("%d: %c --> %c\n", n, from, to);
42     ++count;
43 }

 

截图

 实验6

代码

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

截图

 

posted @ 2023-10-30 17:31  z0y  阅读(10)  评论(0)    收藏  举报