实验2
实验2
task1
问题一:rand函数无法每次运行都生成一个随机数,而srand(time)使之真正随机
问题二:随机抽取一名学生
task2
问题一:去掉后没有重置total_price的值,会使下次运行出错
问题二:继续执行continue后面的循环语句
task3
源代码
#include <stdio.h>
int main() {
char color;
while (scanf(" %c", &color) == 1)
{
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");
}
}
return 0;
}
运行截图
task4
源代码
#include <stdio.h>
int main() {
double x;
double total = 0,max=0,min=20000;
while (1) {
scanf("%lf", &x);
if (x == -1) break;
total += x;
if (x > max) max = x;
if (x < min) min = x;
}
printf("今日累计消费金额: %.1lf 元\n", total);
printf("今日最高一笔开销: %.1lf 元\n", max);
printf("今日最低一笔开销: %.1lf 元\n", min);
return 0;
}
运行截图
task5
源代码
#include <stdio.h>
int main() {
int a, b, c;
while (printf("请输入 a, b, c: ") && scanf("%d %d %d", &a, &b, &c) == 3) {
if (a + b > c && a + c > b && b + c > a) {
if (a == b && b == c) {
printf("等边三角形\n");
}
else if ((a * a + b * b == c * c) ||(a * a + c * c == b * b) ||(b * b + c * c == a * a)) {
printf("直角三角形\n");
}
else if (a == b || a == c || b == c) {
printf("等腰三角形\n");
}
else {
printf("普通三角形\n");
}
}
else {
printf("不能构成三角形。\n\n");
}
}
return 0;
}
运行截图
task6
源代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a, b, c=3;
srand(time(NULL));
b = rand() % 30 + 1;
printf("猜猜2025年11月哪一天是你的 lucky day\n\n");
printf("开始咯,你有三次机会,猜吧(1~30):");
for (int i = 0; i < c; ++i) {
scanf("%d", &a);
if (a == b) {
printf("哇,猜中了;)\n"); break;
}
if ((a != b) && (a > b)) printf("\n你猜的日期晚了,你的 lucky day在前面哦\n\n");
else if ((a != b) && (a < b)) printf("\n你猜的日期早了,你的 lucky day还没到呢\n\n");
if(i<c-1) printf("再猜(1~30):");
}
if(a !=b) printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号", b);
return 0;
}