首页 > 其他分享 >实验2

实验2

时间:2024-10-09 19:02:27浏览次数:1  
标签:%. int random else 实验 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)line21代码 random_no = rand() % (N2 - N1 + 1) + N1; 的功能:

生成一个在区间 [N1, N2] 内的随机数

(2)line25代码 random_no = rand() % N3 + 1; 的功能:

生成一个在区间[1, N3]内的随机数

(3)这个程序的功能是什么:

生成一定数量的随机编号并输出

 

任务2

源代码:

 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 
30     return 0;
31 }

运行结果:

 

任务3

源代码:

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

 

运行结果:

 

 

任务4

源代码:

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

运行结果:

 

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

 

运行结果:

 

任务6

源代码:

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

 

运行结果:

 

标签:%.,int,random,else,实验,printf,include
From: https://www.cnblogs.com/tzrblogs/p/18454743

相关文章

  • 20222327 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    一.实验内容1.了解Linux系统下的基本操作命令,能够处理一些命令出现的error。2.掌握了栈与堆的概念以及在进程内存管理中的应用。3.了解基本的汇编语言指令及其功能。4.能够深刻理解BoF的原理以及如何运用payload完成BoF的攻击二.实验过程任务一直接修改程序机器指令,改变程......
  • 实验二
    任务一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));//以当前系统时间作为随机种子......
  • 实验二
    任务一验证性实验源码#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缓冲区溢出的定义和原因定义:写入缓冲区的数据量超过该缓冲区能容纳的最大限度,造成溢出的数据改写了与该缓冲区相邻的原始数据的情形。原因:(直接)由于代码语言的设计问题、程序员的安全意识问题,程序没有严格的内存越界检查;(根本)冯诺依曼体系的安全......