• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cxj114-514

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

实验三

1.实验任务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, April~";
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 
26     return 0;
27 
28     }
29 
30 
31 void print_spaces(int n)
32 {
33     int i;
34 
35     for(i = 1;i<=n; ++i)
36         printf(" ");
37 }
38 
39 
40 void print_blank_lines(int n)
41 {
42     int i;
43 
44     for(i = 1;i <= n;++i)
45         printf("\n");
46 }
47 
48 
49 void print_text(int line, int col, char text[])
50 {
51     print_blank_lines(line-1);
52     print_spaces(col-1);
53     printf("%s",text);
54 }

在屏幕上二维坐标点随机生成“hi,April~”,单个语句长度为8个字符,N为80,总共生成十个语句。

2.实验任务2

task2_1.c

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

task2_2.c

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

局部static变量的特性:数据存储在静态存储区,函数退出时变量始终存在,再次进入该函数时,将不再重新赋值,直接使用上一次的结果。

3.实验任务3

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 long long func(int n);
 5 
 6 int main()
 7 {
 8     int n;
 9     long long f;
10 
11     while(scanf("%d",&n)!=EOF)
12     {
13         f = func(n);
14         printf("n = %d,f = %lld\n",n,f);
15     }
16 
17     system("pause");
18     return 0;
19 }
20 
21 
22 long long func(int n)
23 {
24     int i=1;
25     long long p=1;
26     for(;i<=n;i++)
27     {
28         p = p * 2;
29 
30     }
31         return p-1;
32 }

4.实验任务4

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

5.实验任务5

task5_1.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 double mypow(int x,int y);
 4 
 5 int main()
 6 {
 7     int x,y;
 8     double ans;
 9 
10     while(scanf("%d%d",&x,&y)!=EOF)
11     {
12         ans = mypow(x,y);
13         printf("%d的%d次方:%g\n\n",x,y,ans);
14     }
15 
16     system("pause");
17     return 0;
18 }
19 
20 double mypow(int x,int y)
21 {
22     double s = 1;
23     int i = 1;
24     if(y>=0)
25         {for(;i<=y;i++)
26             {s = s * x;}
27         }
28     if(y<0)
29     {for(;i<=abs(y);i++)
30         {s = s / x;}
31     }
32 
33     return s;
34 }

task5_2.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 double mypow(int x,int y);
 4 
 5 int main()
 6 {
 7     int x,y;
 8     double ans;
 9 
10     while(scanf("%d%d",&x,&y)!=EOF)
11     {
12         ans = mypow(x,y);
13         printf("%d的%d次方:%g\n\n",x,y,ans);
14     }
15 
16     system("pause");
17     return 0;
18 }
19 
20 double mypow(int x,int y)
21 {
22     double s=1;
23     if(y==0)
24         return 1;
25     if(y>0)
26         {s = x * mypow(x,y-1);
27         return s;}
28     if(y<0)
29         {s = s / mypow(x,-y);
30         return s;}
31 }

6.实验任务6

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

7.实验任务7

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int is_prime(int n);
 5 int main()
 6 {    
 7     int n,i,p = 1,q,p_flag = 1,q_flag = 1;
 8     for(n=2;n<=20;n++)
 9     {    if(n%2 == 1)
10             continue;
11         if(n>2&&n%2 == 0)
12             {    p = 1;
13                 do
14                 {    
15                     p = p + 1;
16                     q = n - p;
17                     p_flag = is_prime(p);
18                     q_flag = is_prime(q);
19                 }while(p_flag * q_flag == 0);
20                 printf("%d = %d + %d\n",n,p,q);
21             }        
22     }
23     system("pause");
24     return 0;
25 }
26 
27 int is_prime(int n)
28 {
29     int i;
30     for(i=2;i<=sqrt(n);++i)
31     {
32         if(n%i==0)
33             break;
34     }
35     if(n>1&&i>sqrt(n))
36         return 1;
37     else
38         return 0;
39 
40 }

8.实验任务8

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 long long func(long s);
 4 int main()
 5 {
 6 
 7     long s,t;
 8 
 9     printf("Enter a numner:");
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     system("pause");
18     return 0;
19 }
20 
21 long long func(long s)
22 {
23     int d;
24     long s1 = 1;
25     long m = 0;
26     while(s>0)
27     {
28         d = s % 10;
29         if(d%2)
30         {
31             m = s1 * d + m;
32             s1 = s1 * 10;
33         }
34         s = s / 10;
35     }
36     return m;
37 }

 

posted on 2023-04-02 12:32  信专某黑化学子  阅读(21)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3