首页 > 其他分享 >实验二

实验二

时间:2024-10-09 17:36:58浏览次数:1  
标签:%. int random 实验 printf include day

任务一

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 #define N1 397
 6 #define N2 476
 7 #define N3 21
 8 int main() {
 9 int cnt;
10 int random_major, random_no;
11 srand(time(NULL)); // 以当前系统时间作为随机种子
12 cnt = 0;
13 while(cnt < N) {
14 random_major = rand() % 2;
15 if(random_major) {
16 random_no = rand() % (N2 - N1 + 1) + N1;//21  将随机数除以(N2 - N1 +1)得到的余数加上 N1后得到的值赋给random_no
17 printf("20248329%04d\n", random_no);
18 }
19 else {
20 random_no = rand() % N3 + 1;//25 将随机数除以(N3)得到的余数加上 1后得到的值赋给random_no
21 printf("20248395%04d\n", random_no);
22 }
23 cnt++;
24 }
25 system("pause");
26 return 0;
27 }//生成随机数,并通过运算输出5个学号

21将随机数除以(N2 - N1 +1)得到的余数加上 N1后得到的值赋给random_no
25 将随机数除以(N3)得到的余数加上 1后得到的值赋给random_no
生成随机数,并通过运算输出5个学号

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

 

任务三

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

 

任务四

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

 

任务五
 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 + b > c && b + c > a) && (a + c > b)){
 6         if((a * a == b * b + c * c||b * b == a * a + c * c)||(c * c == a * a + b * b) )
 7             printf("直角三角形\n");
 8         else{
 9             if((a == b || a == c) || b == c)
10                 if(a==b&&b==c)
11                     printf("等边三角形\n");
12                 else
13                     printf("等腰三角形\n");
14             else
15                 printf("普通三角形\n");
16         }}
17     else
18         printf("不能构成三角形\n");
19 }
20 return 0;
21 }

任务六

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

 

标签:%.,int,random,实验,printf,include,day
From: https://www.cnblogs.com/LittleZcy/p/18454733

相关文章

  • 实验二
    任务一验证性实验源码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));//以当前系统时间作为随机种子......
  • 实验一
    实验任务1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>u......
  • 实验一 C++
    实验任务1:task1.cpp:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213......
  • OOP实验一
    任务1:源码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>5usingnamespacestd;6//声明7//模板函数声明8template<typenameT>9voidoutput(constT&c);10//普通函数声明11voidtes......
  • 实验1 现代C++编程初体验
    任务一#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout<<&qu......
  • 实验1
    #include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout<<&qu......
  • 《DNK210使用指南 -CanMV版 V1.0》第二十八章 音频播放实验
    第二十八章音频播放实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)正点原......
  • 20222407 2024-2025-1《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容1.1.1缓冲区溢出的定义和原因定义:写入缓冲区的数据量超过该缓冲区能容纳的最大限度,造成溢出的数据改写了与该缓冲区相邻的原始数据的情形。原因:(直接)由于代码语言的设计问题、程序员的安全意识问题,程序没有严格的内存越界检查;(根本)冯诺依曼体系的安全......
  • 20222314 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    网络攻防实验报告姓名:陈振烨学号:20222314实验日期:2024/09/29—2024/10/09实验名称:缓冲区溢出和shellcode指导教师:王志强实验要求: 1.掌握NOP,JNE,JE,JMP,CMP汇编指令的......
  • 实验十二 迈克耳孙干涉仪
         ......