晚餐

题目描述

小明请好朋友们来家里吃晚饭,但是厨房里的餐具不够了,于是小明到仓库里找新餐具。仓库里的东西都是装在一个个箱子里,箱子上面写着里面装的什么东西,现在小明想请你帮忙找出这些装餐具的箱子。
题目中的餐具只包含:碗(bowl),刀(knife),叉(fork),筷子(chopsticks)。

输入格式

输入包含多组测试数据。每组先输入一个整数N,表示仓库里有N个箱子。
然后输入N个字符串,分别表示箱子里装的是什么东西。

输出

对于每组输入,输出仓库里所有餐具的名字。

样例输入

3 basketball fork chopsticks
2 bowl letter

样例输出

fork chopsticks

bowl


#include <iostream>
#include <string>

using namespace std;

int main()
{
    string num,str[10];
    int n;
    while(1)
    {
        int count = 0;
        cin>>n;
        for(int j=0;j<n;++j)
        {
            cin>>num;
            if(num=="bowl"||num=="knife"||num=="chopsticks"||num=="fork")
            {
                str[count] = num;
                ++count;
            }
        }
        for(int j=0;j<count;++j)
            cout<<str[j]<<"  ";
        cout<<endl;
    }
    return 0;
}


以下是使用C语言编写的代码实现: ```c #include <stdio.h> #include <string.h> #define MAXN 1005 struct student { char dorm[10]; // 宿舍楼 char room[5]; // 宿舍号 int breakfast; // 早餐订餐数量 int lunch; // 午餐订餐数量 int dinner; // 晚餐订餐数量 } stu[MAXN]; int n; int num_song, num_jv, num_liu; // 松园、菊园、柳园的正确订单数 int breakfast_song, breakfast_jv, breakfast_liu; // 早餐数目 int lunch_song, lunch_jv, lunch_liu; // 午餐数目 int dinner_song, dinner_jv, dinner_liu; // 晚餐数目 // 比较函数,用于排序 int cmp(struct student a, struct student b) { if (strcmp(a.dorm, b.dorm) != 0) { return strcmp(a.dorm, b.dorm) < 0 ? -1 : 1; } else if (strcmp(a.room, b.room) != 0) { return strcmp(a.room, b.room) < 0 ? -1 : 1; } else { return 0; } } // 处理一个园区的订单 void process(struct student stu[], int *num, int *breakfast, int *lunch, int *dinner) { // 将错误的订单分到正确的园区 for (int i = 0; i < n; i++) { if (strcmp(stu[i].dorm, "song") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { (*num)++; *breakfast += stu[i].breakfast; *lunch += stu[i].lunch; *dinner += stu[i].dinner; } } else if (strcmp(stu[i].dorm, "jv") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { (*num)++; *breakfast += stu[i].breakfast; *lunch += stu[i].lunch; *dinner += stu[i].dinner; } } else if (strcmp(stu[i].dorm, "liu") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { (*num)++; *breakfast += stu[i].breakfast; *lunch += stu[i].lunch; *dinner += stu[i].dinner; } } } // 输出该园区的正确订单信息 printf("%d %d %d %d\n", *num, *breakfast, *lunch, *dinner); for (int i = 0; i < n; i++) { if (strcmp(stu[i].dorm, "song") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner); } } else if (strcmp(stu[i].dorm, "jv") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner); } } else if (strcmp(stu[i].dorm, "liu") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner); } } } } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &n); // 读入数据 for (int i = 0; i < n; i++) { scanf("%s %s %d %d %d", stu[i].dorm, stu[i].room, &stu[i].breakfast, &stu[i].lunch, &stu[i].dinner); } // 处理松园的订单 num_song = 0; breakfast_song = 0; lunch_song = 0; dinner_song = 0; process(stu, &num_song, &breakfast_song, &lunch_song, &dinner_song); // 处理菊园的订单 num_jv = 0; breakfast_jv = 0; lunch_jv = 0; dinner_jv = 0; process(stu, &num_jv, &breakfast_jv, &lunch_jv, &dinner_jv); // 处理柳园的订单 num_liu = 0; breakfast_liu = 0; lunch_liu = 0; dinner_liu = 0; process(stu, &num_liu, &breakfast_liu, &lunch_liu, &dinner_liu); // 输出结果 printf("song %d %d %d %d\n", num_song, breakfast_song, lunch_song, dinner_song); qsort(stu, n, sizeof(struct student), cmp); for (int i = 0; i < n; i++) { if (strcmp(stu[i].dorm, "song") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner); } } } printf("jv %d %d %d %d\n", num_jv, breakfast_jv, lunch_jv, dinner_jv); qsort(stu, n, sizeof(struct student), cmp); for (int i = 0; i < n; i++) { if (strcmp(stu[i].dorm, "jv") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner); } } } printf("liu %d %d %d %d\n", num_liu, breakfast_liu, lunch_liu, dinner_liu); qsort(stu, n, sizeof(struct student), cmp); for (int i = 0; i < n; i++) { if (strcmp(stu[i].dorm, "liu") == 0) { if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') { printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner); } } } } return 0; } ``` 这里使用了结构体来存储每个同学的信息,方便后续的处理和排序。在处理每个园区的订单时,先将错误的订单分到正确的园区,然后按照楼号和宿舍号的顺序排序并输出。注意在输出之前,要先按照园区的顺序输出该园区的正确订单数目和订餐数目。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值