2017年PTA乙级秋考B题开学寄语 分数 20 题型:模拟

285 阅读1分钟

PTA | 程序设计类实验辅助教学平台 (pintia.cn)

刚开始写前面的一问:

#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int main()
{
    int n,m;cin>>n>>m;
    int k,temp;
    string id;
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        mp[temp]=1;  //做标记
    }
    for(int i=0;i<n;i++)
    { 
        int flag=0;
         cin>>id>>k;
        for(int j=0;j<k;j++)
        {
            cin>>temp;
            if(mp[temp])
            {
               cout<<id<<": "<<temp<<endl;
            }
        }
    }
    return 0;
}

image.png

Deubug:

我们发现多个违禁品属于一个人的名字只出现一次就够了。所以我们要对名字做个标记,如下:

#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int main()
{
    int n,m;cin>>n>>m;
    int k,temp;
    string id;
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        mp[temp]=1;  //做标记
    }
    for(int i=0;i<n;i++)
    { 
        int flag=0;
         cin>>id>>k;
        for(int j=0;j<k;j++)
        {
            cin>>temp;
            if(mp[temp])
            {
                if(!flag)
                {
                    cout<<id<<":";
                    flag=1;
                }
                printf(" %4d ",temp);
            }
        }
        if(flag)printf("\n");
    }
    return 0;
}

image.png

OK,现在我们来写第二问:

#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int main()
{
    int n,m;cin>>n>>m;
    int k,temp,person=0,goods=0;
    string id;
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        mp[temp]=1;  //做标记
    }
    for(int i=0;i<n;i++)
    { 
        int flag=0;
         cin>>id>>k;
        for(int j=0;j<k;j++)
        {
            cin>>temp;
            if(mp[temp])
            {
                if(!flag)
                {
                    cout<<id<<":";
                    flag=1;
                }
                printf(" %04d", temp);
                goods++;
            }
        }
        if(flag)
        {
            printf("\n");
            person++;
        }
        
    }

    printf("%d %d\n",person,goods);
    return 0;
}

image.png