实验2

任务1

问题1:生成一个随机数

问题2:生成一个随机学号

 

任务2

问题1:会把上次运行的结果加进去

问题2:继续循环

 任务3

#include <stdio.h>

int main() {
    char ch;
    while ((ch = getchar()) != EOF) {
        if (ch == '\n') {
            continue;
        }
        switch (ch) {
            case 'r':
                printf("stop!\n");
                break;
            case 'g':
                printf("go go go\n");
                break;
            case 'y':
                printf("wait a minute\n");
                break;
            default:
                printf("something must be wrong..\n");
                break;
        }
    }
    return 0;
}

image

 

 

任务4

#include <stdio.h>

int main() {
    double expense, total = 0, max = 0, min = 20000;
    printf("输入今日开销,直到输入-1终止:\n");
    while (1) {
        scanf("%lf", &expense);
        if (expense == -1) {
            break;
        }
        total += expense;
        if (expense > max) {
            max = expense;
        }
        if (expense < min) {
            min = expense;
        }
    }
    printf("今日累计消费总额:%.1f\n", total);
    printf("今日最高一笔开销:%.1f\n", max);
    printf("今日最低一笔开销:%.1f\n", min);
    return 0;
}

image

 

 

任务5

#include <stdio.h>

int main() {
    int a, b, c;
    while (scanf("%d%d%d", &a, &b, &c) != EOF) {
        if (a > b) { int t = a; a = b; b = t; }
        if (a > c) { int t = a; a = c; c = t; }
        if (b > c) { int t = b; b = c; c = t; }
        if (a + b <= c) {
            printf("不能构成三角形\n");
        } else {
            if (a == b && b == c) {
                printf("等边三角形\n");
            } else if (a == b || b == c || a == c) {
                printf("等腰三角形\n");
            } else if (a * a + b * b == c * c) {
                printf("直角三角形\n");
            } else {
                printf("普通三角形\n");
            }
        }
    }
    return 0;
}

image

 

 

任务6

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

int main() {
    srand(time(NULL));
    int luckyDay = rand() % 30 + 1;
    int guess, count = 0;
    printf("猜猜2025年11月哪一天是你的lucky day\n");
    while (count < 3) {
        printf("开始喽,你有三次机会,猜吧(1~30):");
        scanf("%d", &guess);
        count++;
        if (guess == luckyDay) {
            printf("哇,猜中了:)\n");
            break;
        } else if (guess > luckyDay) {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        } else {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        }
        if (count < 3) {
            printf("再猜(1~30):");
        }
    }
    if (count == 3 && guess != luckyDay) {
        printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", luckyDay);
    }
    return 0;
}

image

 

posted @ 2025-10-16 18:55  0721绫地宁宁  阅读(5)  评论(0)    收藏  举报