首页 > 其他分享 >实验2

实验2

时间:2024-10-13 14:59:17浏览次数:1  
标签:%. int random else 实验 printf include

任务1:

源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 #define N 5
 5 #define N1 397
 6 #define N2 476
 7 #define N3 21
 8 
 9 int main()
10 {
11     int cnt;
12     int random_major,random_no;
13     
14     srand(time(NULL));    //seed
15     
16     cnt = 0;
17     while(cnt < N)
18     {
19         random_major = rand() % 2;
20         
21         if(random_major)
22         {
23             random_no = rand() % (N2 - N1 + 1) + N1;
24             printf("20248329%04d\n",random_no);
25         
26         }
27         else
28         {
29             random_no = rand() % N3 + 1;
30             printf("20248329%04d\n",random_no);
31         }
32     
33     cnt++;
34     }
35     
36 return 0;
37 }

 问题1:将随机数值控制在N1~N2

问题2:将随机数值控制在1~N3

问题3:随机选取五个学号

 

 

 

任务2:

源代码:

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 int main()
 5 {
 6     double a,b,c;
 7     double delta,p1,p2;
 8     
 9     while(scanf("%lf%lf%lf",&a,&b,&c) != EOF)
10     {
11         if(a == 0)
12         {
13             printf("a = 0,invalid input\n");
14             continue;
15         }
16         
17         delta = b*b-4*a*c;
18         p1 = -b/2/a;
19         p2 = sqrt(fabs(delta))/2/a;
20         
21         if(delta == 0)
22         {
23             printf("x1 = x2 = %.2g\n",p1);
24         }
25         
26         else if(delta > 0)
27         {
28             printf("x1 = %.2g , x2 = %.2g\n", p1+p2,p1-p2);
29         }
30         
31         else
32         {
33             printf("x1 = %.2g + %.2gi, ",p1,p2);
34             printf("x2 = %.2g - %.2gi\n",p1,p2);
35         }
36             
37     }
38     
39     return 0;
40 }

 

 

任务3:

源代码:

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

 

 

任务4:

源代码:

 

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

 

 

任务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             printf("不能构成三角形\n");
11         }
12         
13         else if(a*a + b*b == c*c||b*b + c*c == a*a||a*a + c*c == b*b)
14         {
15             printf("直角三角形\n");
16         }
17         
18         else if (a == b == c)
19         {
20             printf("等边三角形\n");
21         }
22         
23         else if (a == b || b == c || a == c)
24         {
25             printf("等腰三角形\n");
26         }
27         
28         else
29         {
30             printf("普通三角形\n");
31         }
32         
33     }
34     return 0;
35 }

 

 

任务6:

源代码:

 

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

 

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

相关文章

  • 实验1 现代C++编程初体验
    Task1code1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 【大数据技术基础 | 实验二】Linux基础:常用基本命令和文件操作
    文章目录一、实验目的二、实验要求三、实验环境四、常用基本命令1、验证cd和pwd命令2、验证ls命令3、验证mkdir命令4、验证cp、mv和rm命令五、Linux文件操作1、验证touch命令2、验证cat命令3、验证more命令六、实验心得一、实验目的学会linux常用命令(cd,ls,pwd......
  • 实验2 c语言分支与循环基础应用编程-1
    实验任务1task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314s......
  • 实验2
    任务1源代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(......
  • 实验2 c语言分支与循环基础应用编程1
    task1:问题1随机数求余后结果为1,生成0397到0476中的随机数问题2随机数求余后结果为0,生成0001到0021中的随机数问题3随机生成5个不同的学号task2: 实验3: task4:1#include<stdio.h>2intmain()3{4doublex,sum,max,min;5sum=0;6......
  • 实验2
    源代码1:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<......
  • 实验1 现代C++编程初体验
    实验任务1:代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;7template<typenameT>8voidoutput(constT&c);9voidtest1();10voidtest2();11vo......
  • 实验2
    点击查看代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));//以当前系统时间作为随机种子cnt=......
  • 实验2
    任务1 1#include<stdio.h>2#include<stdlib.h>3#include<math.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time......
  • 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第一......