首页 > 其他分享 >实验2

实验2

时间:2024-10-10 14:48:56浏览次数:1  
标签:21 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:生成一个数字使它在397到476之间。

问题2:生成一个数字使它在1到21之间。

问题3:随机生成班级内的的学号。     任务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;
29 }

 

任务3

源代码

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

任务4

源代码

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

任务5

源代码

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

任务6

源代码

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

 

 

 

标签:21,int,random,else,实验,printf,include
From: https://www.cnblogs.com/shansuiqu/p/18454931

相关文章

  • 20222419 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容(1)了解了缓冲区溢出发展历史:红色代码、冲击波病毒、震荡波病毒、心脏出血、乌克兰断网、勒索病毒。(2)了解了缓冲区溢出漏洞的本质和危害:缓冲区溢出漏洞是由于程序没有进行严格的内存越界检查,导致数据溢出并覆盖相邻内存空间,从而可能被攻击者利用执行恶......
  • 实验1 现代c++基础编程
    任务1:源代码task1.cpp//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include......
  • 实验1 c++
    任务1:task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//......
  • 实验二
    任务一源代码1#include<stdio.h>2#include<time.h>34#defineN55#defineN13976#defineN24767#defineN3218intmain(){910intrandom_major,random_no;11intcnt;12srand(time(NULL));1314cnt=......
  • 实验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(......
  • 实验2
    任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time(......
  • 为何实验室、课题组选择够快云库管理科研资料?
    够快云库提供文件管理服务时,很多高校、科研机构的实验室、课题组选择用够快云库管理组内的文件、资料。和一般团队不同,实验室、课题组的文件以科研文献、实验数据为主,对安全要求高;课题组成员之间文献、实验数据同步、协作需求频繁;还涉及论文的收集、修改……使用中,够快云库......
  • 实验2
    1#include<stdio.h>2#include<math.h>3#include<time.h>4#defineN55#defineN13976#defineN24767#defineN3218intmain()9{10intcnt;11intrandom_major,random_no;12srand(time(NULL));13cnt=0;14while(c......
  • 实验1 C++
    任务1:task.cpp:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//......
  • 实验1 C++
    任务1:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9......