实验2

1.一:生成五个202400420001~202400420100范围内的随机数并输出
二:生成一个一到一百之间的随机数
三:使数字输出时以四位数输出,当数字不足四位时,在数字前补零凑足四位
四:以当前系统时间作为随机数种子,让rand()生成的伪随机数每次运行程序都不同;若去掉这行,多次运行程序会生成完全相同的随机数序列。

代码

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;
printf("20490042%04d\n", number);
}
return 0;
}
截图

2

一:下一个顾客购买时,会在上一个顾客的购买金额上继续累加,导致顾客购买时金额出错
二:跳过本次循环进入下一次循环

代码

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

2

3.代码

include<stdio.h>

include<stdlib.h>

int main(){
char color;
while(scanf("%c",&color)==1){
while(getchar()!='\n');
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;
}
}
system("pause");
return 0;
}
截图

2

4.代码

include <stdio.h>

include <stdlib.h>

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

system("pause");
return 0;
}
截图

2

5.代码

include <stdio.h>

include <stdlib.h>

int main() {
int a,b,c,t;

while (scanf("%d %d %d", &a, &b, &c) != EOF) {

if (a > b) {
t = a;
a = b;
b = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
if (a > b) {
t = a;
a = b;
b = t;
}

if (a + b <= c)
printf("不能构成三角形\n");
else {
if (a == b && b == c)
printf("等边三角形\n");
else if (a == b || b == c)
if (aa + bb == c*c)
printf("直角三角形\n");
else
printf("等腰三角形\n");

else if (aa + bb == c*c)
printf("直角三角形\n");
else
printf("普通三角形\n");
}
}

system("pause");
return 0;
}
截图

2

6.代码

include <stdio.h>

include <stdlib.h>

int main() {
int lucky_day = 1 + rand() % 30;

int guess,i;
int attempts = 3;

printf("猜猜2026年4月哪一天是你的lucky day\n");
printf("开始喽,你有3次机会,猜吧(1-30):");

for (i = 1; i <= attempts; i++) {
scanf("%d", &guess);

if (guess == lucky_day) {
printf("哇,猜中了:)\n");
break;
}
else if (guess < lucky_day)
printf("你猜的日期早了,你的lucky day还没到呢\n");
else
printf("你猜的日期晚了,你的lucky day在前面哦\n");

if (i < attempts)
printf("再猜(1-30):");
}
if (i >attempts) {
printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day);
}
system("pause");
return 0;
}
截图

2

posted @ 2026-04-06 17:43  xialkQwQ  阅读(14)  评论(0)    收藏  举报