天天看点

PAT 乙级1072 开学寄语 (20分)

1072 开学寄语 (20分)

下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面、理发、整衣,然后思过、读书、锻炼、明智、开悟、精进。而后必成大器也!

本题要求你写个程序帮助这所学校的老师检查所有学生的物品,以助其成大器。

输入格式:

输入第一行给出两个正整数 N(≤ 1000)和 M(≤ 6),分别是学生人数和需要被查缴的物品种类数。第二行给出 M 个需要被查缴的物品编号,其中编号为 4 位数字。随后 N 行,每行给出一位学生的姓名缩写(由 1-4 个大写英文字母组成)、个人物品数量 K(0 ≤ K ≤10)、以及 K 个物品的编号。

输出格式:

顺次检查每个学生携带的物品,如果有需要被查缴的物品存在,则按以下格式输出该生的信息和其需要被查缴的物品的信息(注意行末不得有多余空格):

姓名缩写: 物品编号1 物品编号2 ……

最后一行输出存在问题的学生的总人数和被查缴物品的总数。

输入样例:

输出样例:

#include <iostream>
#include <map>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    map<string,bool> store;
    string temp;
    for (int i = 0; i < m; ++i) {
        cin>>temp;
        store[temp] = true;
    }
    string name;
    int num,cnt_stu,cnt_goods;
    bool flag,flag_endl;
    cnt_stu = cnt_goods = 0;
    flag_endl = false;
    for (int i = 0; i < n; ++i) {
        cin>>name>>num;
        flag = false;
        for (int j = 0; j < num; ++j) {
            cin>>temp;
            if(store[temp]){
                if(!flag){
                    if(flag_endl)
                        cout<<endl;
                    else
                        flag_endl = true;
                    cout<<name<<":";
                    flag = true;
                    cnt_stu++;
                }
                cnt_goods++;
                cout<<" "<<temp;
            }
        }
    }
    if(flag_endl)//如果一个都没有没收,则上面没输入,即不需要换行。不加这个判断,会有一个测试点格式错误
        cout<<endl;
    cout<<cnt_stu<<" "<<cnt_goods;
    return 0;
}