实验2

text 1 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 100
 6 #define CNT 5
 7 
 8 int main() {
 9     int number;
10     int i;
11 
12     srand(time(0));     
13     for(i = 0; i < CNT; ++i) {
14         number = rand() % N + 1;
15         printf("20490042%04d\n", number);
16     }
17 
18     return 0;
19 }

运行截图

image

 问题1:抽取随机5个学员

问题2:得到一个1~100的整数

问题3:不足4位左边补0

问题4:使输出结果随机

text 2

 1 #include<stdio.h>
 2 int main(){
 3     int choice,quantity;
 4     float total_price=0,amount_paid,change;
 5     while(1){
 6         printf("\n自动饮料售卖机菜单:\n");
 7         
 8         printf("1.可乐-3元/瓶\n");
 9         printf("2.雪碧-3元/瓶\n");
10         printf("3.橙汁-5元/瓶\n");
11         printf("4.矿泉水-2元/瓶\n");
12         printf("0.退出购买流程\n");
13         printf("请输入饮料编号:\n");
14         scanf("%d",&choice);
15         if (choice==0)
16            break;
17         if(choice<1||choice>4){
18             printf("无效的饮料编号,请重新输入。\n");
19             continue;
20         }
21         printf("请输入购买的数量:");
22         scanf("%d",&quantity);
23         if (quantity<0){
24             printf("购买数量不能为负数,请重新输入。\n");
25             continue;
26         } 
27         if(choice==1||choice==2)
28             total_price+=3*quantity;
29         else if(choice==3)
30           total_price+=5*quantity;
31           else
32             total_price+=2*quantity;
33         printf("请投入金额:");
34         scanf("%f",&amount_paid);
35         change=amount_paid-total_price;
36         printf("本次购买总价:%.2f元\n",total_price);
37         printf("找零:%2.f元\n",change);
38         total_price=0; 
39     }
40     printf("感谢您的购买,欢迎下次光临!\n");
41     return 0;
42 
43 } 

效果截图

image

 问题1:total_price会不断累加

问题2:结束本次循环开始下一轮

text 3

 1 #include<stdio.h>
 2 int main(){
 3     char light;
 4     int i;
 5     for(i=0;i<4;i++){
 6     printf("请输入信号灯(g/r/y):");
 7     scanf(" %c",&light);
 8     if(light=='g')
 9        printf("go go go\n");
10     else if(light=='y')
11        printf("wait a minute\n");
12     else if(light=='r')
13       printf("stop\n");
14     else
15       printf("something must be wrong\n");
16 }
17       return 0;
18 }

效果截图

image

 text 4

 1 #include<stdio.h>
 2 int main(){
 3     double money;
 4     double sum =0;
 5     double max,min;
 6     int count=0;
 7     printf("输入今日开销,直到输入-1终止:\n");
 8     while(1)
 9     {scanf("%lf",&money);
10     if (money==-1){
11         break;
12     }
13         count++;
14     if (count==1){
15         max=money;
16         min=money;
17     }
18     sum=sum+money;
19     if(money>max)
20       max=money;
21     if(money<min)
22     min=money;
23     }
24     if(count==0)
25     {printf("你还没有输入任何开销记录\n");
26     
27     }
28     else
29     {printf("今日累计消费总额:%.1lf\n",sum);
30     printf("今日最高一笔开销:%.1lf\n",max);
31     printf("今日最低一笔开销:%.1lf\n",min);
32     }
33     return 0;
34 }

效果截图

image

 text 5

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int a, b, c;
 5     int i;
 6     for (i = 0; i < 5; i++)
 7     {
 8         scanf("%d %d %d", &a, &b, &c);
 9         if (a + b > c && a + c > b && b + c > a)
10         {
11             if (a == b && b == c)
12             {
13                 printf("等边三角形\n");
14             }
15             else if (a == b || a == c || b == c)
16             {
17                 printf("等腰三角形\n");
18             }
19             else if ((a*a + b*b == c*c) || (a*a + c*c == b*b) || (b*b + c*c == a*a))
20             {
21                 printf("直角三角形\n");
22             }
23             else
24             {
25                 printf("普通三角形\n");
26             }
27         }
28         else
29         {
30             printf("不能构成三角形\n");
31         }
32     }
33     return 0;
34 }

效果截图

image

 text 6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
    int lucky_day, guess;
    int chance;
    srand((unsigned)time(NULL));
    lucky_day = rand() % 30 + 1;
    printf("猜猜2026年11月哪一天是你的lucky day\n");
    printf("开始喽,你有3次机会,猜吧(1~30): ");
    for(chance = 1; chance <= 3; chance++)
    {
        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(chance < 3)
        {
            printf("再猜(1~30): ");
        }
        else
        {
            printf("次数用光啦。11月你的lucky day是%d号\n", lucky_day);
        }
    }
    return 0;
}

效果截图

image

 

image

 

posted @ 2026-10-31 14:58  杜庆昊  阅读(6)  评论(0)    收藏  举报