实验2

任务1
源代码task1.c

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 100
#define CNT 5

int main() {
    int number;
    int i;

    srand(time(0));     // 以当前系统时间作为随机种子
    for(i = 0; i < CNT; ++i) {
        number = rand() % N + 1;
        printf("20490042%04d\n", number);
    }

    return 0;
}

运行测试截图
image

问题回答
问题1:随机生成五个学员编号
问题2:得到1到100内的一个随机整数作为学号后四位
问题3:生产四位数字不足的用0补足
问题5:以时间为种子生成不同的数字

task2.c
源代码

点击查看代码
#include <stdio.h>
int main() {
    int choice, quantity;
    float total_price = 0, amount_paid, change;

    while (1) {
        printf("\n自动饮料售卖机菜单:\n");
        printf("1. 可乐 - 3 元/瓶\n");
        printf("2. 雪碧 - 3 元/瓶\n");
        printf("3. 橙汁 - 5 元/瓶\n");
        printf("4. 矿泉水 - 2 元/瓶\n");
        printf("0. 退出购买流程\n");
        printf("请输入饮料编号: ");
        scanf("%d", &choice);

        if (choice == 0)
            break;

        if (choice < 1 || choice > 4) {
            printf("无效的饮料编号,请重新输入。\n");
            continue;
        }

        printf("请输入购买的数量: ");
        scanf("%d", &quantity);

        if (quantity < 0) {
            printf("购买数量不能为负数,请重新输入。\n");
            continue;
        }

        if(choice == 1 || choice == 2)
            total_price += 3 * quantity;
        else if(choice == 3)
            total_price += 5 * quantity;
        else
            total_price += 2 * quantity;

        printf("请投入金额: ");
        scanf("%f", &amount_paid);

        change = amount_paid - total_price;
        printf("本次购买总价: %.2f 元\n", total_price);
        printf("找零: %.2f 元\n", change);

        total_price = 0;
    }
    printf("感谢您的购买,欢迎下次光临!\n");
    return 0;
}

运行测试截图
image

问题回答
问题1:每次的总价会叠加在一起
问题2:跳过本次循环,直接进入下次循环

task3.c
源代码

点击查看代码
#include <stdio.h>
int main(){
    char color;
    for(int i=1;i<=4;i++){
        scanf(" %c",&color);
        switch(color){
            case 'r':
                printf("stop!\n");
                break;
            case 'y':
                printf("wait a minute\n");
                break;
            case 'g':
                printf("go go go\n");
                break;
            default:
                printf("something must be wrong....\n");
                break;
        }
    }
    return 0;
}

运行测试截图
![image](https://img2024.cnblogs.com/blog/3861625/202611/3861625-20261102104540496-643731721.png)

问题回答

task4.c
源代码
<details>
<summary>点击查看代码</summary>

include <stdio.h>

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

</details>

运行测试截图
image

问题回答

task5.c
源代码

点击查看代码
#include <stdio.h>
int main()
{
    float n;
    float total = 0.0;
    float max = 0.0;
    float min = 0.0;
    int first = 1;
    printf("输入今日开销,直到输入-1终止:\n");
    while (1)
    {
        scanf("%f", &n);
        if (n == -1)
            break;
        total += n;
        if (first){
            max = n;
            min = n;
            first = 0;
        }else{
            if (n > max)
                max = n;
            if (n < min)
                min = n;
        }
    }
    printf("今日累计消费总额: %.1f\n", total);
    printf("今日最高一笔开销: %.1f\n", max);
    printf("今日最低一笔开销: %.1f\n", min);
    return 0;
}

运行测试截图
image

问题回答

task6.c
源代码

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int lucky;
    int guess;
    int i;
    printf("猜2026年11月哪一天是你的lucky day\n");
    srand(time(0));
    lucky = rand() % 30 + 1;
    for (i = 1; i <= 3; i++)
    {
        if (i == 1)
            printf("开始喽,你有3次机会,猜吧(1~30): ");
        else
            printf("再猜(1~30): ");
        scanf("%d", &guess);
        if (guess == lucky){
            printf("哇,猜中了:)\n");
            return 0;
        }
        else if (guess < lucky){
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        }
        else{
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }
    }
    printf("次数用光啦。11月你的lucky day是%d号\n", lucky);
    return 0;
}

运行测试截图
image

问题回答

posted @ 2026-11-02 10:55  陈翼飞  阅读(2)  评论(0)    收藏  举报