实验2

实验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("20248395%04d\n", random_no);
        }
        cnt++;
    }
    return 0;
}

结果:

 

 回答:

line21:随机生成一个397~476之间的数

line25:随机生成一个1~21之间的数

功能:随机生成5个学号

实验任务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 w;
    while(scanf("%c",&w)!=EOF){
        if(w=='r'){
            getchar();
            printf("stop!\n");
        }
        else if(w=='g'){
            getchar();
            printf("go go go\n");
        }
        else if(w=='y'){
            getchar();
            printf("wait a minute\n");
        }
        else{
            getchar();
            printf("something must be wrong...\n");
        }
    }
    return 0;
}

结果:

 实验任务4:

源代码:

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     double expense, maxexpense = 0, minexpense = 20000, totalexpense = 0;
 6     printf("输入今日开销,直到输入-1终止:\n");
 7     while (1) {
 8         scanf("%lf", &expense);
 9         if (expense == -1) {
10             break;
11         }
12         if (expense > maxexpense) {
13             maxexpense = expense;
14         }
15         if (expense < minexpense) {
16             minexpense = expense;
17         }
18         totalexpense += expense;
19     }
20     printf("最高一笔开销:%.1lf元\n", maxexpense);
21     printf("最低一笔开销:%.1lf元\n", minexpense);
22     printf("一天总开销:%.1lf元\n", totalexpense);
23     return 0;
24 }

结果:

 实验任务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         if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
 8             printf("不能构成三角形\n");
 9         }
10         else {
11             if ((a == b) && (b == c)){
12                 printf("等边三角形\n");
13             }
14             else if ((a == b) || (a == c) || (b == c)) {
15                 printf("等腰三角形\n");
16             }
17             else if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a)) {
18                 printf("直角三角形\n");
19             }
20             else {
21                 printf("普通三角形\n");
22             }
23         }
24     }
25     return 0;
26 }

结果:

 实验任务6:

源代码:

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

结果:

 

 

posted @ 2024-10-11 13:59  KinderECHO  阅读(9)  评论(0编辑  收藏  举报