pat甲级A1022 Digital Library (30分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336

 

 

 

参考代码:

#include <iostream>
#include <map>
#include <set>
 
using namespace std;
 
map<string, set<int> > title, author, key, pub, year;//构造从书名,作者,关键字,出版社,出版年份到书籍ID的映射
 
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, m, id, num;
    scanf("%d", &n);
    string ttitle, tauthor, tkey, tpub, tyear;
    for(int i = 0; i < n; i++) {
        scanf("%d\n", &id);
        getline(cin, ttitle);// 书名中可能含有空格,所以要用getline
        title[ttitle].insert(id);// 插入到映射中 映射键值id
        getline(cin, tauthor);
        author[tauthor].insert(id);
        while(cin >> tkey) { // cin不接受换行符,可以作为输入的终点
            key[tkey].insert(id);
            char c = getchar();
            if(c == '\n') break;
        }
        getline(cin, tpub);
        pub[tpub].insert(id);
        getline(cin, tyear);
        year[tyear].insert(id);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%d: ", &num);
        string temp;
        getline(cin, temp);
        cout << num << ": " << temp << "\n";
        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);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值