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

实验1运行结果

实验1代码功能

在根据时间作随机种子选出的随机数行除以25取余,列除以80取余,打出空格和换行后打出“hi,november!”

实验2.1源代码

 1 #include<stdio.h>
 2 
 3 long long fac(int n);
 4 
 5 int main()
 6 {
 7     int i,n;
 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 {
18     static long long p = 1;
19     printf("p=%lld\n",p);
20     p = p*n;
21     return p;
22 }

实验2.1运行结果

实验2.2源代码

 1 #include<stdio.h>
 2 int func(int,int);
 3 
 4 int main()
 5 {
 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 int func(int a,int b)
15 {
16     static int m=0,i=2;
17     
18     i += m + 1;
19     m = i + a + b;
20     
21     return m;
22 }

实验2.2运行结果

实验3源代码

 1 #include<stdio.h>
 2 long long func(int n);
 3 
 4 int main()
 5 {
 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     return 0;
15 }
16 
17 long long func(int n)
18 {
19     int i,s;
20     long long f;
21     for(i=1,s=1;i<=n;++i)
22         s=s*2;
23     
24     f=s-1;
25     return f;
26 }

实验3运行结果

 

实验4.1源代码

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

实验4.1运行结果

实验4.2源代码

 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 {
16     if(m>n)
17         return 0;
18     else if(m==n||m==0)
19         return 1;
20     else
21         return func(n-1,m)+func(n-1,m-1);
22 }

实验4.2运行结果

 

实验5源代码

 

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

实验5运行结果

 

实验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     int n;
22     long sum = 0;
23     while (s > 0) 
24     {
25         n = s % 10;
26         if (n % 2 != 0) 
27         {
28             sum = sum * 10 + n;
29         }
30         s /= 10;
31     }
32 
33     return sum;
34 }

实验6运行结果

 

 

posted @ 2023-11-05 12:02  天要下鱼吗  阅读(16)  评论(0)    收藏  举报