实验2

#任务1
##源代码
#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<n;++i){
    number=rand()%100+1;
    printf("20490042%04d\n",number);
}  
  return 0;
}
问题回答:1该代码的作用是生成一个1到100的随机整数
2它的功能是生成一个 1 到 100 之间的随机整数
3%04d 用于格式化输出整数
4设置随机数
#实验2
##源代码
```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如果去掉这行代码,当你进行第二次及以后的购买时,total_price 会保留上一次的金额并继续累加,导致计算出的总价和找零金额全部错误
2跳回循环开头重新执行
#任务3
##源代码
```c
#include<stdio.h>
int main(){
  char n,r,g;
  scanf("c",&n);
  if(n=='r')
    printf("%s","stop!");
  else if(n=='g')
    printf("%s","go go go");
  else
    printf("%s","wait a minute");
  return 0;
}
#任务4
##源代码
```c
#include <stdio.h>
int main() {
    double expense;
    double total = 0.0;
    double max_expense = -1.0;
    double min_expense = 999999.0; 
    int count = 0;
    printf("输入今日开销,直到输入-1终止:\n");
    while (1) {
        scanf("%lf", &expense);
        if (expense == -1) {
            break;
        }
        if (expense <= 0 || expense > 20000) {
            printf("输入金额无效,请重新输入(0 < 金额 <= 20000):\n");
            continue;
        }

        total += expense;
        if (expense > max_expense) {
            max_expense = expense;
        }
        if (expense < min_expense) {
            min_expense = expense;
        }
        count++;
    }
    if (count > 0) 
        printf("今日累计消费总额: %.1f\n", total);
        printf("今日最高一笔开销: %.1f\n", max_expense);
        printf("今日最低一笔开销: %.1f\n", min_expense);
     else 
        printf("未输入任何有效开销。\n");
    return 0;
}
#实验5
##源代码
```c
#include <stdio.h>
int main() {
    int a, b, c;
    int n = 5; 
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &a, &b, &c);
        if (a + b <= c || a + c <= b || b + c <= a) {
            printf("不能构成三角形\n");
        } else {
            if (a == b && b == c) {
                printf("等边三角形\n");
            } 
            else if (a == b || a == c || 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 {
                printf("普通三角形\n");}
        }
    }
    return 0;
}
#实验6
##源代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int lucky_day, guess;
    int max_chances = 3;
    srand((unsigned)time(NULL));
    lucky_day = rand() % 30 + 1;
    printf("猜猜2026年11月哪一天是你的lucky day\n");
    printf("开始喽,你有3次机会,猜吧(1~30):");
    for (int i = 0; i < max_chances; i++) {
        scanf("%d", &guess);
        if (guess == lucky_day) {
            printf("哇,猜中了:)\n");
            return 0; 
        } else if (guess > lucky_day) {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        } else {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        }
        if (i < max_chances - 1) {
            printf("再猜(1~30):");
        }
    }
    printf("次数用光啦,11月你的lucky day是%d号\n", lucky_day);
    return 0;
}
posted @ 2026-11-03 22:15  沉默的日夕  阅读(3)  评论(0)    收藏  举报