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分题了,主要考察的是对于字符串的输入,剩下就是读题和格式化输出,很简单