实验2

试验任务1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define N 5
#define N1 397
#define N2 476
#define N3 21

int main(){
    int cnt;
    int random_major,random_no;
    
    srand(time(NULL));
    
    cnt = 0;
    while(cnt<N){
        random_major = rand()%2;
        
        if(random_major){
           random_no = rand()%(N2-N1+1)+N1;
           printf("20248329%04d\n",random_no);
    }
    else{
        random_no =rand()%N3 + 1;
        printf("20248329%04d\n",random_no);
    }
    
    cnt++;
    }
    return 0;
}

问题一:随机获得N1到N2之间的一个三位数

问题二:随机生成1到21之间的一个整数

问题三:在11班,12班,奇安信班所有学号中随机生成一个学号

实验任务2

#include <stdio.h>
#include <math.h>

int main() {
    double a, b, c;
    double delta, p1, p2; 

    while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
        if(a == 0) {
            printf("a = 0, invalid input\n");
            continue;
        }

        delta = b*b - 4*a*c;
        p1 = -b/2/a;
        p2 = sqrt(fabs(delta))/2/a;

        if(delta == 0)
            printf("x1 = x2 = %.2g\n", p1);
        else if(delta > 0)
            printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2);
        else {
            printf("x1 = %.2g + %.2gi, ", p1, p2);
            printf("x2 = %.2g - %.2gi\n", p1, p2);
        }
    }

    return 0;
}

 

实验任务3

#include<stdio.h>
int main()
{
    char a;
    do{
    scanf("%c",&a);
    getchar();
    if (a=='y')
    {
    printf("wait a minute\n");
     } 
     
     else if(a=='g')
     {
     printf("go go go\n");
     }
     
     else if(a=='r')
     {
         printf("stop!\n");
     }
     
     else 
         printf("something must be wrong...\n");
    }
    
    while(1);
    
    
    return 0;
    
}

 

实验任务4


 


 1 #include<stdio.h>
 2 int main()
 3 {
 4     float sum=0;
 5     float num;
 6     float min=20000;
 7     float max=0;
 8     
 9     printf("输入今日开销,直到-1终止\n"); 
10      
11         while(scanf("%f",&num)!=EOF){
12         
13         
14         if (num!=-1)
15         {
16         
17         sum+=num;
18         
19         if(num>max)
20         {
21         max=num;
22         }
23         else
24         {
25         max=max;
26         }
27         if(num<min)
28         {
29         min=num;    
30         }
31         else
32         {
33         min=min;
34         }
35           continue;
36         }
37         
38         else
39         {
40         printf("今日累计消费:%.1f\n",sum); 
41             printf("今日最高开销:%.1f\n",max);
42             printf("今日最低开销:%.1f\n",min);
43         break;
44         }
45         }
46         
47         
48     return 0;
49     
50 }

 


 

实验任务5


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

 


 

 

 

实验任务6


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

 

 

 

posted @ 2024-10-10 22:42  王昊祺  阅读(11)  评论(0编辑  收藏  举报