首页 > 其他分享 >实验2

实验2

时间:2024-10-09 20:49:32浏览次数:1  
标签:int random else 实验 printf include day

任务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作用是取397~476之间的随机数

问题2

line25作用是取1~21之间的随机数

问题3

程序的功能是在202483290397~202483290476和202483950001~2024839590021之间取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("x2 = %.2g - %.2gi\n", p1, p2);
25         }
26     }
27 
28     return 0;

 运行结果

 

任务3

 

源代码

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

 

运行结果

 

任务4

 

源代码

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

 

运行结果

 

任务5

 

源代码

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

 

运行结果

 

任务6

 

源代码

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

 

运行结果

 

标签:int,random,else,实验,printf,include,day
From: https://www.cnblogs.com/oliveira/p/18454734

相关文章

  • 实验2
    Task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(tim......
  • 实验2 C语言分支与与循环基础应用编程——1
    一、实验目的1.能正确使用if语句实现分支结构2.能正确使用while语句、do...while语句实现循环结构3.能在具体问题场景中正确区分、使用continue和break4.能灵活、组合使用c语句编程解决简单应用问题 二、实验准备1.分支语句if和循环语句while、do...while的用法......
  • 实验1 现代C++编程初体验
    task1:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#includ......
  • 实验1 现代C++编程初体验
    任务一源代码1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13v......
  • 实验1 现代C++编程初体验
    1.实验任务11#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函数声明......
  • 实验2
    任务1源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(......
  • 实验二 C语言分支与循环基础应用编程
    实验二C语言分支与循环基础应用编程实验任务1——抽学号#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){ intcnt; intrandom_major,random_no; srand(time(NULL));//以当前系统......
  • 实验2
    task1点击查看代码#include<stdio.h>#include<stdlib.h>#include<math.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){ intcnt; intrandom_no,random_major; srand(time(NULL)); cnt=0; while(cnt<N) {......
  • 实验2
    任务1:源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand......
  • 20222327 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    一.实验内容1.了解Linux系统下的基本操作命令,能够处理一些命令出现的error。2.掌握了栈与堆的概念以及在进程内存管理中的应用。3.了解基本的汇编语言指令及其功能。4.能够深刻理解BoF的原理以及如何运用payload完成BoF的攻击二.实验过程任务一直接修改程序机器指令,改变程......