pat甲级1022(30分)c++

pat甲级1022(30分)c++

类型:字符串输入处理

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

/*
 * 题目大意:
 * 第一行:
 * N表示书的总数量
 * 接下来N*6行:
 * 第一行是书的7位ID
 * 第二行是书名
 * 第三行是书的作者
 * 第四行是书的关键词
 * 第五行是书的出版商
 * 第六行是书的发布年
 * 接下来一行:
 * M是查询的次数
 * 接下来M行:
 * 按照给定的格式(使用最开始的数字标识使用书的哪个属性进行查询)
 * 输出:
 * 对每次查询附加上查询结果
 */

//map以ID为值,其余属性分别作为键,ID存放在set中
map<string, set<int> > title, author, key, pub, year;
/*
 * query方法是对m进行查询str,并输出
 */
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);
        title[ttitle].insert(id);
        getline(cin, tauthor);
        author[tauthor].insert(id);
        while(cin >> tkey) {
            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;
}

这道题应该是最简单的30分题了,主要考察的是对于字符串的输入,剩下就是读题和格式化输出,很简单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值