首页 > 其他分享 >实验3

实验3

时间:2023-11-05 12:11:20浏览次数:37  
标签:int long char 实验 func printf include

实验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运行结果

 

 

标签:int,long,char,实验,func,printf,include
From: https://www.cnblogs.com/almost2223/p/17798414.html

相关文章

  • 实验3 类与数组、指针
    实验任务1point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmov......
  • 实验三 类与数组、指针
    实验任务1task1.cpp源码:Point.hpp:1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13......
  • 实验3
    任务1源代码1#include<stdio.h>2#include<time.h>3#include<windows.h>4#include<stdlib.h>5#defineN806voidprint_line(intn);7voidprint_col(intn);8voidprint_text(intline,intcol,chartext[]);9intmain(){10......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain......
  • 第三次实验
    任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain(){ ......
  • 实验3
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intco1,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,co1,i;......
  • 数据采集与融合技术实验四
    作业①:要求:使用Selenium框架+MySQL数据库存储技术路线爬取“沪深A股”、“上证A股”、“深证A股”3个板块的股票数据信息。主要代码url_list=['http://quote.eastmoney.com/center/gridlist.html#hs_a_board','http://quote.eastmoney.com/center/gridlist......
  • 实验3 C语言函数应用编程
    任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn......
  • 实验3 类与数组、指针
    实验任务1Point.hpp1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()co......
  • 实验三
    test1#include<stdio.h>#include<time.h>#include<stdlib.h>#include<windows.h>#defineN80voidtest_(intline,intcol,chartest[]);voidspace(intn);voidblank(intn);intmain(){intline,col,i;chartest[N]=&......