首页 > 其他分享 >实验2

实验2

时间:2024-10-11 15:34:24浏览次数:1  
标签:%. int else cost 实验 printf include

任务一:

代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 #define N1  397
 7 #define N2  476
 8 #define N3  21
 9 
10 int main(){
11     int cnt;
12     int random_major,random_no;
13     
14     srand(time(NULL));
15     
16     cnt = 0;
17     while(cnt<N){
18         random_major = rand()%2;
19         
20         if(random_major){
21             random_no=rand() %(N2-N1+1)+N1;
22             printf("20248329%04d\n",random_no);
23         }
24         else{
25             random_no=rand()%N3+1;
26             printf("20248395%04d\n",random_no);
27         }
28         
29         cnt++;
30     }
31     return 0;
32 }

截图

Q1:将随机学号限定在11和12班里

Q2:将学号限定在奇安信班里

Q3:随机抽取学号

 

 

任务二:

代码

 1 //一元二次方程求解
 2 
 3 #include<stdio.h>
 4 #include<math.h>
 5 
 6 int main(){
 7     double a,b,c;
 8     double delta,p1,p2;
 9     
10     while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){
11         if(a==0){
12             printf("a=0,invalid input\n");
13             continue;
14         }
15         
16         delta=b*b-4*a*c;
17         p1=-b/2/a;
18         p2=sqrt(fabs(delta))/2/a;
19         
20         if(delta==0)
21             printf("x1=x2=%.2g\n",p1);
22         else if(delta > 0)
23             printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2);
24         else {
25             printf("x1 = %.2g + %.2gi, ", p1, p2);
26             printf("x2 = %.2g - %.2gi\n", p1, p2); 
27         }
28     } 
29     return 0;
30 }
31     

截图

 

 

任务三:

代码

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char colour;
 5     while(scanf("%c",&colour)!=EOF)
 6     {
 7         if(colour=='r')
 8         {    printf("stop!\n");}
 9         else if(colour=='g')
10         {    printf("go go go\n");}
11         else if(colour=='y')
12         {    printf("wait a minute\n");}
13         else
14         {    printf("something must be wrong...\n");}
15         getchar( );
16     }
17     return 0;
18 }

截图

 

 

 

任务四:

代码

 1 #include<stdio.h>
 2 int main()
 3 {
 4     double cost,sum=1,max=0,min=20000;
 5     printf("输入今日开销,输入-1时终止:\n"); 
 6     while(cost!=-1)
 7     {
 8         scanf("%lf",&cost);
 9         sum=(sum+cost);
10     
11         if(cost>max)
12             max=cost;
13             
14         if(min>cost&&cost!=-1)
15             min=cost;
16         
17     }
18     printf("今日累计消费总额: %.1f\n",sum);
19     printf("今日最高一笔开销:%.1f\n",max);
20     printf("今日最低一笔开销:%.1f\n",min);
21     return 0;
22 }

截图

 

 

任务五:

代码

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a,b,c;
 5     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
 6     {
 7         if(a==b&&b==c)
 8             printf("等边三角形\n"); 
 9             
10         else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
11             printf("直角三角形\n");
12             
13         else if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a))
14             printf("等腰三角形\n");
15         
16         else if((a+b<c)||(a+c<b)||(b+c<a))
17             printf("不能构成三角形\n");
18             
19         else
20             printf("普通三角形\n");     
21     }
22     return 0; 
23 }

截图

 

任务六:

代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 int main()
 6 {
 7     int a, trym = 3, try1 = 0;
 8     srand(time(NULL));
 9     int random = rand() % 30 + 1;
10     printf("猜猜2024年11月哪一天会是你的lucky day\n");
11     printf("开始咯,你有三次机会,猜吧(1-30):");
12 
13     while (try1 < trym)
14     {
15         scanf("%d", &a);
16         try1++; 
17 
18         if (a < random)
19         {
20             printf("你猜的日期早了,你的lucky day还没到呢\n");
21             printf("再猜(1-30):");
22         }
23         else if (a > random)
24         {
25             printf("你猜的日期晚了,你的lucky day在前面哦\n");
26             printf("再猜(1-30):");
27         }
28         else if (a == random) 
29         {
30             printf("哇,猜中了:)\n");
31             break; 
32         }
33     }
34 
35     if (try1 >= trym)
36         printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", random);
37 
38     return 0;
39 }

截图

 

标签:%.,int,else,cost,实验,printf,include
From: https://www.cnblogs.com/202483290470yyq/p/18458514

相关文章

  • 数据结构实验第六周
    6-1在一个数组中实现两个堆栈原理就是共享栈,不会的可以看我的数据结构博客StackCreateStack(intMaxSize){StackS=(Stack)malloc(sizeof(structSNode));//这个初始化记得写S->Top1=-1,S->Top2=MaxSize;//栈满的条件S->MaxSize=MaxSize;S->Data=(int......
  • Vchitect 2.0:上海 AI 实验室推出的视频生成利器
    目录引言一、Vchitect2.0模型概述二、Vchitect2.0的技术特点1.超分辨率功能2.帧插入功能3.基于文本和图像的生成能力三、Vchitect2.0的应用场景四、Vchitect2.0测评报告五、生成视频样例六、本地部署推理1.环境准备2.安装依赖3.模型推理结语引言随......
  • 实验2
    任务1源代码1#include<time.h>2#defineN53#defineN13974#defineN24765#defineN3216intmain(){7intcnt;8intrandom_major,random_no;910srand(time(NULL));1112cnt=0;13while(cnt<N){14random_major=rand()%......
  • 实验1 Linux操作系统的安装
    目录1.实验目的和要求:2.主要仪器设备:3.实验内容和原理:(1)实验内容:(2)实验原理:4.操作方法和实验步骤(绘图):5.实验结果与分析(1)将登陆后的效果截图证明系统安装成功(2)回答问题:安装Linux系统一般需要哪些分区?(3)文件和目录类命令的使用(写下你所使用的命令)(4)系统信息类命令......
  • 实验2
    1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45intmain(){6intluckyday,x,y=3;78printf("猜猜2024年11月哪一天会是你的luckyday\n");910srand(time(NULL));11luckyday=rand()......
  • 实验2
    实验2实验任务1:源代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));//以当前系统时间作为随机种子cnt......
  • 20222415 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习了缓冲区溢出的相关知识,介绍了缓冲区和缓冲区溢出,并介绍了几种缓冲区溢出的实现方式。2.实验过程1.直接修改程序机器指令,改变程序执行流程1.1将目标文件pwn20222415通过共享文件夹放到kali里,运行pwn20222415.1.2输入objdump-dpwn1|more对pwn20222415......
  • 20222319zzs 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1知识回顾1.1.1什么是缓冲区溢出?计算机中,如果程序试图向一个缓冲区填充超出它能够容纳的数据,溢出的数据可能会覆盖其他重要的内存区域,导致程序运行失败甚至崩溃,如果这些溢出数据是精心设计的.则攻击者就可以利用它们指向预先设计的攻击代码(shellcode)(Shellcode是核......
  • 实验1 现代C++编程初体验
    实验任务1代码1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9......
  • 实验1 现代C++编程初体验
    任务1代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#inc......