首页 > 其他分享 >实验2

实验2

时间:2024-10-11 16:48:04浏览次数:1  
标签:p1 源代码 int while 实验 printf include

任务1:

源代码:

 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 
32     return 0;
33 }

问题1:生成在[N1,N2]范围内的随机数

问题2:生成在[1,N3]范围内的随机数

问题3:生成并输出5个随机的学生学号,学号有两种格式,

  • 以20248329开头的学号后接一个4位数,范围从397到476。
  • 以20248395开头的学号后接一个4位数,范围从1到21。

 

任务2:

源代码:

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

 

任务3:

源代码:

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

 

任务4:

源代码:

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

 

任务5:

源代码:

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

 

任务6:

源代码:

 

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

 

标签:p1,源代码,int,while,实验,printf,include
From: https://www.cnblogs.com/wjz08/p/18455167

相关文章

  • 实验2
    任务1源代码:编译 任务2源代码: 编译 任务3源代码:编译任务4源代码; 编译 任务5源代码: 编译任务6源代码:编译 ......
  • 20222302 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习内容1.熟练掌握了栈和堆的概念。2.掌握了Linux的基本操作,如shell命令和编译器gcc、调试器gdb的使用。3.掌握了缓冲区溢出的原理。实验任务本次实验的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何......
  • 20222425 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习了缓冲区溢出的相关知识点,介绍了包括汇编语言、进程内存管理、函数调用过程等相关知识点。1.1实践目标1.手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数。2.利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发getShell函数。3.注入......
  • 20222324 石国力《网络与系统攻防技术》实验一
    1.1实践目标本次实践的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串。该程序同时包含另一个代码片段,getShell,会返回一个可用Shell。正常情况下这个代码是不会被运行的。我们实践的目标就是想办法运行这个......
  • 实验二
    实验一:源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13976#defineN24767#defineN3218intmain(){9intcnt;10intrandom_major,random_no;11srand(time(NULL));12......
  • 实验二
    task11.line21代码作用:随机生成在N1和N2之间的数字2.line25代码作用:随机生成在1和N3之间的数字3.该程序功能:随机生成五个两个班学生学号task2 task3task4 task5task6 ......
  • 实验2
    实验任务1:task1.c源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain()11{12intcnt;13intrandom_major,random_no;14......
  • 实验2
    任务一:代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand......
  • 数据结构实验第六周
    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.模型推理结语引言随......