实验二

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
int number;
int i;
srand(time(0)); // 以当前系统时间作为随机种子
for(i = 0; i < N; ++i) {
number = rand() % 100 + 1; // 生成1-100的随机数
printf("20490042%04d\n", number); // 将随机数格式化为4位数,不足前面补零
}
return 0;
}
//生成202400420001~202400420100的学号 

#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; // break 直接终止当前循环 
if (choice < 1 || choice > 4) {
printf("无效的饮料编号,请重新输入。\n");
continue;
}
printf("请输入购买的数量: ");
scanf("%d", &quantity);
if (quantity < 0) {
printf("购买数量不能为负数,请重新输入。\n");
continue; // continue跳过当前循环的剩余代码,直接进入下一次循环 
}
switch (choice) { // 需要 没有限制choice的范围  
case 1:
case 2:
total_price += 3 * quantity;
break;
case 3:
total_price += 5 * quantity;
break;
case 4:
total_price += 2 * quantity;
break;
}
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;
}

#include <stdio.h>
int main(){
    char color;
    while(scanf("%c", &color) != EOF){
        switch(color){
         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;
        }
    }
    printf("程序已终止\n");
    return 0;
}

#include <stdio.h>
int main(){
    double expense;
    double total = 0;
    double max = 0;
    double min  = 20000;
    printf("每日开销: \n");
    while (1){
        scanf("%lf", &expense);
        if(expense == -1){
            break;
        }
        if(expense <= 0 , expense > 20000){
            printf("无效 \n");
            continue;
        }
        total += expense;
        if (expense > max){
            max = expense;
        }
        if (expense < min){
            min = expense;
        }
    }
    printf("\n=== 今日开销统计 ===\n");
    printf("最高开销: %.1f元\n", max);
    printf("最低开销: %.1f元\n", min);
    printf("总开销: %.1f元\n", total);
    
    return 0;
}

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

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

#include <stdio.h>

void print_person(int spaces) {
    printf("%*s O \n", spaces, "");
    printf("%*s<H>\n", spaces, "");
    printf("%*sI I\n", spaces, "");
}

int main() {
    int n, i, j, spaces;
    
    printf("input n: ");
    scanf("%d", &n);
    
    for (i = n; i >= 1; i--) {
        spaces = (n - i) * 2;
        
        for (j = 0; j < i; j++) {
            print_person(spaces);
            spaces += 6;
        }
    }
    
    return 0;
}

 

posted @ 2025-04-21 02:56  胡博淞  阅读(7)  评论(0)    收藏  举报