实验二

test1

#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 < CNT; ++i) {
        number = rand() % N + 1;
        printf("20490042%04d\n", number);
    }

    return 0;
}

test1
A1:程序的功能是随机生成学号后两位
A2:生成数字并模100保证学号后两位在0-99
A3:用0补位保证后四位中,前两位是00,后两位是随机生成的数
A4:以时间为随机数种子,保证每次运行的随机性

test2

#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;
}

test2

A1:每次购买的价格累计,重复付款
A2:在这个情景中,当输入不合法(编号超过范围和购买数量不为正)时使用continue,跳过本次循环,进入下一次循环

test3

#include<stdio.h>
signed main()
{
	for(int i=1;i<=4;i++)
	{
		char x;
		scanf("%c",&x);
		getchar();
		if(x=='r') printf("stop!\n");
		else if(x=='g') printf("go go go\n");
		else if(x=='y') printf("wait a minute\n");
		else printf("something must be wrong...\n");
	}
	return 0;
}

test3
test4

#include<stdio.h>
signed main()
{
	double mi=20000000,mx=0,sum=0;
		printf("输入今日开销,直到输入-1终止:\n");
	while(1)
	{
		double x;
		scanf("%lf",&x);
		getchar();
		if(x==-1) break;
		if(x<mi) mi=x;
		if(x>mx) mx=x;
		sum+=x;
	}
	printf("今日最高开销:%lf\n",mx);
	printf("今日最低开销:%lf\n",mi);
	printf("今日累计消费:%lf\n",sum);
	return 0;
}

test4
test5

#include<stdio.h>
#include<math.h>
void swap(int *a,int *b)
{
	*a^=*b;*b^=*a;*a^=*b;
}
signed main()
{
	int T=5;
	while(T--)
	{
		int a,b,c;
		scanf("%d %d %d",&a,&b,&c);
		if(a>b) swap(&a,&b);
		if(b>c) swap(&b,&c);
		if(a>b) swap(&a,&b);
		if(a+b<=c)
		{
			printf("不能构成三角形\n");
		}
		else
		{
			bool flag=0;
			if(a==b&&a!=b) printf("等腰"),flag=1;
			if(a==b&&b==c) printf("等边"),flag=1;
			if(a*a+b*b==c*c) printf("直角"),flag=1;
			if(!flag) printf("普通");
			printf("三角形\n");
		}
	}
	return 0;
}

test5
test6

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int get(int l,int r)
{
	return rand()*rand()%(r-l+1)+l;
}
signed main()
{
	srand(time(0));
	printf("猜猜2026年11月哪一天是你的lucky day\n");
	int d=get(1,30);
	printf("\n开始了你有3次机会(1-30):");
	for(int i=1;i<=3;i++)
	{
		int x;
		scanf("%d",&x);
		getchar();
		if(d<x)
		{
			printf("\n猜的日期晚了,你的lucky day在前面哦\n");
		}
		else if(d>x)
		{
			printf("\n猜的日期早了,你的lucky day在后面哦\n");
		}
		else
		{
			printf("\n哇 猜中了!");
			break;
		}
		if(i<3) printf("\n再猜(1-30):");
		else printf("\n次数用光了 你的lucky day是%d号",d);
	}
	return 0;
}

test6
test6_2

posted @ 2026-10-28 09:39  Jessica2333  阅读(7)  评论(0)    收藏  举报