首页 > 其他分享 >实验三

实验三

时间:2023-11-01 21:22:23浏览次数:32  
标签:main int long 实验 func printf include

task.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);  // 暂停1000ms
22     }
23     
24     return 0; 
25 }
26 
27 // 打印n个空格 
28 void print_spaces(int n) {
29     int i;
30     
31     for(i = 1; i <= n; ++i)
32         printf(" ");
33 }
34 
35 // 打印n行空白行
36 void print_blank_lines(int n) {
37     int i;
38     
39     for(i = 1; i <= n; ++i)
40         printf("\n");
41  } 
42 
43 // 在第line行第col列打印一段文本 
44 void print_text(int line, int col, char text[]) {
45     print_blank_lines(line-1);      // 打印(line-1)行空行 
46     print_spaces(col-1);            // 打印(col-1)列空格
47     printf("%s", text);         // 在第line行、col列输出text中字符串
48 }

功能:每隔一秒在下面随机行,随机列打印字符串

task.2-1

 1 // 利用局部static变量的特性,计算阶乘
 2 
 3 #include <stdio.h>
 4 long long fac(int n); // 函数声明
 5 
 6 int main() {
 7     int i, n;
 8 
 9     printf("Enter n: ");
10     scanf("%d", &n);
11 
12     for (i = 1; i <= n; ++i)
13         printf("%d! = %lld\n", i, fac(i));
14 
15     return 0;
16 }
17 
18 // 函数定义
19 long long fac(int n) {
20     static long long p = 1;
21 
22     p = p * n;
23 
24     return p;
25 }

 

task.2-2

 1 // 练习:局部static变量特性
 2 
 3 #include <stdio.h>
 4 int func(int, int);        // 函数声明
 5 
 6 int main() {
 7     int k = 4, m = 1, p1, p2;
 8 
 9     p1 = func(k, m);    // 函数调用
10     p2 = func(k, m);    // 函数调用
11     printf("%d, %d\n", p1, p2);
12 
13     return 0;
14 }
15 
16 // 函数定义
17 int func(int a, int b) {
18     static int m = 0, i = 2;
19 
20     i += m + 1;
21     m = i + a + b;
22 
23     return m;
24 }

总结:ststic 变量是动态的,会改变,下一次该变量的值是上一次参与运算后的结果

task.3

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

task.4-1

 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     int i,j,a=1,b=1;
15     if(n<m)
16     return 0;
17     if(n==m||m==0)
18     return 1;
19     for(i=1;i<=m;i++)
20         a*=i;
21     for(j=n;j>=n-m+1;j--)
22         b*=j;
23     return b/a;
24 }

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

 

task.5

 1 #include<stdio.h>
 2 void hanoit(int n,char a,char b,char c);
 3 int main()
 4 {
 5     int n,s,i;
 6     while(scanf("%d",&n)!=EOF)
 7     {
 8         s=0;
 9         hanoit(n,'A','B','C');
10         for(i=1;i<=n;i++)
11         s=2*s+1;
12         printf("一共进行了%d次\n",s);
13     }
14     return 0;
15 }
16 
17 
18 void hanoit(int n,char a,char b,char c){
19     if(n==1)
20     printf("1:%c-->%c\n",a,c);
21     else 
22     {
23         hanoit(n-1,a,c,b);
24         printf("%d:%c-->%c\n",n,a,c);
25         hanoit(n-1,b,a,c);
26     }
27     
28     
29 }

 

task.6

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

 task.7

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

 

标签:main,int,long,实验,func,printf,include
From: https://www.cnblogs.com/lei1459/p/17798401.html

相关文章

  • 实验3 C语言函数应用编程
    任务1源码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_bl......
  • 实验三
    作业①要求:指定一个网站,爬取这个网站中的所有的所有图片,例如:中国气象网(http://www.weather.com.cn)。使用scrapy框架分别实现单线程和多线程的方式爬取。–务必控制总页数(学号尾数2位)、总下载的图片数量(尾数后3位)等限制爬取的措施。输出信息:将下载的Url信息在控制台输出,并将下......
  • 城市时空预测的统一数据管理和综合性能评估 [实验、分析和基准]《Unified Data Manage
    2023年11月1日,还有两个月,2023年就要结束了,希望在结束之前我能有所收获和进步,冲呀,老咸鱼。 摘要解决了访问和利用不同来源、不同格式存储的不同城市时空数据集,以及确定有效的模型结构和组件。1.为城市时空大数据设计的统一存储格式“原子文件”,并在40个不同的数据集上验证了其......
  • LIMS系统源码:从样品登记到检验全面管理实验室流程
    LIMS可用于管理完整的实验程序,从样品登记到检验、校核、审核到最终批准报告,建立在过程质量控制的基础上,对检测流程进行有效全面的管理,对影响质量的人、机、料、法、环因素加以控制,同时为质量改进提供数据依据。LIMS实验室信息管理系统,功能包括以下几个模块:委托管理:样品登记、样品接......
  • 实验3_c语言函数应用编程
    task1#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(){intline,col......
  • 实验3
    task1 源代码1#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(int......
  • 软件设计实验4:抽象工厂模式
    实验4:抽象工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解抽象工厂模式的动机,掌握该模式的结构;2、能够利用抽象工厂模式解决实际问题。 [实验任务一]:人与肤色使用抽象工厂模式,完成下述产品等级结构: 实验要求:1. 画出对应的类图; 2. 提交源代......
  • 软件设计实验5:建造者模式
    实验5:建造者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解建造者模式的动机,掌握该模式的结构;2、能够利用建造者模式解决实际问题。 [实验任务一]:计算机组装使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起......
  • 软件设计实验1:UML与面向对象程序设计原则
    实验1:UML与面向对象程序设计原则本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、掌握面向对象程序设计中类与类之间的关系以及对应的UML类图;2、理解面向对象程序设计原则。 [实验任务一]:UML复习阅读教材第一章复习UML,回答下述问题:面向对象程序设计中类与类的关......
  • 软件设计实验2:简单工厂模式
    实验2:简单工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解简单工厂模式的动机,掌握该模式的结构;2、能够利用简单工厂模式解决实际问题。 [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参......