实验三

task.1

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 {
11     int line, col, i;
12     char text[N] = "hi, November~";
13     srand(time(0)); // 以当前系统时间作为随机种子
14     for(i = 1; i <= 10; ++i) {
15     line = rand() % 25;
16     col = rand() % 80;
17     print_text(line, col, text);
18     Sleep(1000); // 暂停1000ms
19 }
20     system("pause");
21     return 0;
22 }
23 void print_spaces(int n)
24 {
25     int i;
26     for(i = 1; i <= n; ++i)
27         printf(" ");
28 }
29 // 打印n行空白行
30 void print_blank_lines(int n)
31 {
32     int i;
33     for(i = 1; i <= n; ++i)
34         printf("\n");
35 }
36 // 在第line行第col列打印一段文本
37 void print_text(int line, int col, char text[])
38 {
39     print_blank_lines(line-1); // 打印(line-1)行空行
40     print_spaces(col-1); // 打印(col-1)列空格
41     printf("%s", text); // 在第line行、col列输出text中字符串
42 }

 

2.运行结果

3.功能

在屏幕上随机行随机位置打印主函数中的字符串

task.2_1

1.源代码

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

 

2.运行结果

 

 增加一行源代码后,会将每次调用函数前p的值打印出来

task.2_2

1.源代码

 

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

预测结果:因为两次调用中静态变量m,i的值被保存,所以p1,p2不一致p1=8;p2=17

2.运行结果

 

局部变量特征:只能在局部函数中被调用,不同函数中同名的局部变量不影响

 

task.3

1.源代码

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

 

2.运行结果

 

 

 

task.4

1.源代码

 迭代

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

递归

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

 

2.运行结果

 

task.5

1.源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 void move(unsigned int n,char from,char to)
 5 {
 6     printf("%u:%c-->%c\n",n,from,to);
 7 }
 8 void hn(unsigned int n,char from,char temp,char to)
 9 {
10     if(n==1)
11         move(n,from,to);
12     else
13         {hn(n-1,from,to,temp);
14         move(n,from,to);
15         hn(n-1,temp,from,to);}
16 }
17 int main()
18 {
19     unsigned int n;
20     double a;
21     while(scanf("%u",&n)!=EOF)
22     {a=pow(2,n*1.0)-1;
23     hn(n,'A','B','C');
24     printf("一个移动了%.0lf次\n",a);}
25     system("pause");
26     return 0;
27 }

 

2.运行结果

 

 

 

task.6

1.源代码

 

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

 

2.运行结果

 

 

task.7

1.源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<math.h>
 4 int main()
 5 {
 6     int n[10];
 7     int i=32,j,a,b,temp;
 8     while(1)
 9     {
10         j=0;
11         a=pow(i,2.0);
12         b=pow(i,3.0);
13         for(;j<4;j++)
14         {
15             n[j]=a%10;
16             a/=10;
17         }
18         for(;j<10;j++)
19         {
20             n[j]=b%10;
21             b/=10;
22         }
23         for(int x=0;x<8;x++)
24         {
25             for(int y=0;y<9-x;y++)
26             {
27                 if(n[y]>n[y+1])
28                 {
29                     temp=n[y];
30                     n[y]=n[y+1];
31                     n[y+1]=temp;
32                 }
33             }
34         }
35         if(n[0]==0&&n[1]==1&&n[2]==2&&n[3]==3&&n[4]==4&&n[5]==5&&n[6]==6&&n[7]==7&&n[8]==8&&n[9]==9)
36             break;
37         i++;
38     }
39     printf("%d\n",i);
40     system("pause");
41     return 0;
42 }

 

2.运行结果

 

posted @ 2023-10-30 17:14  JUVBuffon  阅读(34)  评论(0)    收藏  举报