NUIST 《程序设计基础》 实验2

实验2~

task1.c

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define N1 80
#define N2 35
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() % N1 + 1;
            printf("20256343%04d\n", random_no);
        }
        else {
            random_no = rand() % N2 + 1;
            printf("20256136%04d\n", random_no);
        }
        cnt++;
    }
    return 0;
}

ROUND1
图片
ROUND2
图片
ROUND3
图片
(2与3注释Line10)

问题1:生成随机数种子,让Rand()生成随机数据

问题2:生成以20256136/20256343开头,4位的随机数,先生成一次随机数,为奇数则20256343开头,再生成随机数对80求余+1;为偶数则20256136开头,再生成随机数对35求余+1;

求余的作用确定位数(?)
查阅资料后发现,rand()对INT类型默认生成0-RAND_MAX (这玩意好像不固定?) 的随机数,对80和35求余,获得0~79或0~34的随机数,再通过%04d固定位数,不足4位在前面补0,+1避免生成0的情况


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;
}

图片

问题1:变量未置零,下次购买时应付款会算上上次的总价
问题2:跳出本次循环,进行下次循环


task3.c

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

void deal(char color) {
    if(color == 'r')
        printf("stop!\n");
    else if(color == 'g')
        printf("go go go\n");
    else if(color == 'y')
        printf("wait a minute\n");
    else
        printf("something must be wrong...\n");
}

int main() {
    char color;
    while (scanf(" %c", &color) != EOF)
    {
        deal(color);
    }
    return 0;
}

图片
图片


task4.c

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

图片

(好像多输了一个“请”...)


task5.c

点击查看代码
#include <stdio.h>
int main() {
    int a,b,c;
    while(scanf("%d %d %d", &a, &b, &c) != EOF) {
        if(a == b && b == c && a == c)
            printf("等边三角形");
        else if(a==b || b==c || a==c)
            printf("等腰三角形");
        else if(a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a) 
            printf("直角三角形");
        else if(a+b>c && b+c>a && a+c>b)
            printf("普通三角形");
        else
            printf("不能构成三角形");
        printf("\n");
    }
    return 0;
}

图片

(等腰直角按什么算?我这儿直接按等腰了)
....哦原来没有边长为整数的等腰直角三角形

task6.c

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

int main() {
    srand(time(NULL));
    printf("猜猜2025年11月哪天是你的lucky day\n\n开始喽,你有三次机会,猜吧(1~30):");
    int day = rand() % 30 + 1; //对30取余+1,固定数据范围在1~30内
    for(int i = 0; i < 3; i++) {
        int guess;
        scanf("%d", &guess);
        if(guess < day) 
            printf("\n你猜的日期早了,你的lucky day还没到呢\n\n");
        else if(guess > day)
            printf("\n你猜的日期晚了,你的lucky day在前面呢\n\n");
        else if(guess == day) {
            printf("\n哇,猜中了:)");
            return 0;
        }
        else
            printf("你输入了什么鬼东西...\n\n");
    }
    printf("次数用光。偷偷告诉你,11月你的lucky day是%d号", day);
}

图片

图片

少换行更正:图片

可折叠代码块好好用啊

posted @ 2025-10-13 16:59  安杰awa  阅读(24)  评论(1)    收藏  举报