首页 > 其他分享 >实验2

实验2

时间:2024-10-13 10:33:23浏览次数:1  
标签:%. int money else 实验 printf include

任务1
 
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.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 }

 

line21 从397开始到476之间随机抽取学号补充到20248329后

line25 从1开始到第22随机抽取学号补充到2024839504后

程序功能:随机抽取学号

 任务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 #include<conio.h>
 3 #define r
 4 #define y
 5 #define g
 6 int main()
 7 {
 8     char c;
 9     while(scanf("%c",&c)!=EOF){
10     if(c=='r'){
11         printf("stop!\n");
12         getchar();
13     }
14     else if(c=='g'){
15         printf("go go go\n");
16         getchar();
17     }
18     else if(c=='y'){
19         printf("wait a minute\n");
20         getchar();
21     }
22     else{
23         printf("something must be wrong...\n");
24         getchar();
25     }
26 }
27     return 0;
28 }

 

 

 任务4

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

 任务5

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

 任务6

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

 

标签:%.,int,money,else,实验,printf,include
From: https://www.cnblogs.com/liqihao/p/18454645

相关文章

  • VLAN-IP实验
    需求:1.PC1和PC3所在接口为access接口;属于VLAN2PC2-4-5-6处于同一网段:其中PC2可以访间Pc4-5-6PC4可以访间Pc5不能访间PC6Pc5不能访问Pc63.PC1-Pc3---192.168.0.024与PC2-4-5-6不在一个网段--192.168.1.0244.所有Pc均使用DHcp禁取IP地址,且PC1可以正常访间Pc2-4-5-6第一......
  • 实验2
    实验1代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain()11{12intcnt;13intrandom_major,random_no;14srand(time(N......
  • 实验2
    #include<stdio.h>#include<malloc.h>#defineSIZE100#defineINCREMENT_SIZE10#defineTRUE1#defineFALSE-1#defineOK1#defineERROR-1typedefintStatus;typedefintElemType;typedefstructLNode{ElemTypedata;structLNode*next;}LNode,*LLi......
  • 实验项目3 自定义路由转换器
    实验目的了解Django处理HTTP请求的流程。掌握路由转换器的用法。掌握如何定义和使用自定义路由转换器。实验内容操作1 创建Django项目chapter02(先进入之前创建的虚拟环境(python3.7、有Django))操作2 在项目chapter02中创建应用app01(应用需要激活应用并分配根路由、创建子......
  • 实验1
    任务1:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 实验2
    task.1#include<stdio.h>#include<stdio.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(c......
  • 实验一 现代C++编程初体验
    case1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usi......
  • 实验1 现代C++编程初体验
    实验1现代C++编程初体验 task1:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参6#include<iostream>7#include<strin......
  • 实验1 现代C++基础编程
    task1实验代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3......
  • 《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)正点原......