首页 > 其他分享 >实验2

实验2

时间:2024-10-14 13:11:11浏览次数:1  
标签:%. int random p1 实验 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:

随机生成在0397-0476的数字。

问题2:

随机生成在0001-0021的数字。

问题3:

随机生成5个学号。

 

任务2:

源代码

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

任务3:

源代码

 

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

任务4:

源代码

 

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

任务5:

源代码

 

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

任务6:

源代码

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

 

标签:%.,int,random,p1,实验,printf,include
From: https://www.cnblogs.com/hydrangea1123/p/18454742

相关文章

  • 【华三】【华三】VXLAN典型组网-集中式网关配置实验
    【华三】VXLAN典型组网-二层组网静态配置实验VXLAN的相关术语VTEP(VXLANTunnelEndPoint)VXLAN隧道核心设备VSI(VirtualSwitchInstance)VSIinterfaceVXLANsegmentVNI(VXLANNetworkIdentifier)AC(AttachmentCircuit)VXLAN隧道工作模式L2Gateway:二层转发模式IPGateway:......
  • 实验2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<N){random......
  • 实验二
    task_1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<......
  • 操作系统:实验二:进程调度
    1.实验目的  1.了解三级调度的基本概念;  2.通过对先来先服务算法、时间片轮转算法和短作业优先算法的模拟实现掌握它们的工作原理。2.三级调度的基本概念  现在的操作系统大多是多道程序系统,在同一时段内,允许用户同时执行多个作业进(或进程)。一个作业从提......
  • 操作系统:实验三:存储器管理
    1.实验目的  1.理解固定式分区及可变式分区两种存储管理模式,知道各自的优缺点;  2.验证型实验需理解可变式分区方式的三种算法最先适应算法、最佳适应算法和最坏适应算法的工作原理,理解内存释放的具体实现过程;  3.提高型实验需在提供的代码框架下根据提示自主......
  • 实验1 现代c++编程初体验
    任务1:task1.cpp//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>......
  • 实验1
    实验任务1代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#......
  • 面向对象程序设计-实验1
    任务一: #include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout&l......
  • 实验1 现代c++编程初体验
    实验任务一task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参6#include<iostream>7#include<string>8#includ......
  • openssl实验截图记录
                                    ......