其他分享
首页 > 其他分享> > PAT甲级1022

PAT甲级1022

作者:互联网

solution

#include <iostream>
#include <cstring>
#include <map>
#include <set>
using namespace std;
map<string, set<int>> title, author, key, pub, year;
void query(map<string, set<int>> &m, string &str)
{
    if (m.find(str) != m.end())
    {
        for (auto it = m[str].begin(); it != m[str].end(); it++)
        {
            printf("%07d\n",*it);
        }
    }
    else cout<<"Not Found\n";
}
int main()
{
    int n;
    scanf("%d\n", &n);
    for (int i = 0; i< n; i++)
    {
        int id;
        scanf("%d\n", &id);
        string a;
        getline(cin, a);
        title[a].insert(id);
        getline(cin, a);
        author[a].insert(id);
        while (cin >> a)
        {
            key[a].insert(id);
            char c=getchar();
            if(c=='\n')break;
        }
        getline(cin, a);
        pub[a].insert(id);
        getline(cin, a);
        year[a].insert(id);
    }
    int m;
    scanf("%d\n", &m);
    while (m--)
    {
        int num;
        scanf("%d: ", &num);
        string temp;
        getline(cin, temp);
        cout << num << ": "<<temp<<endl;
       if(num == 1) query(title, temp);
        else if(num == 2) query(author, temp);
        else if(num == 3) query(key, temp);
        else if(num == 4) query(pub,temp);
        else if(num ==5) query(year, temp);

    }
}

标签:insert,PAT,1022,int,scanf,cin,getline,甲级,id
来源: https://blog.csdn.net/qq_34832548/article/details/120117434