定义id时将id定义为了int型,但是id有可能为 00001234这种前导为0的一串数字,故输出时要保持长度为7。
#include<iostream>
#include<map>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
const int maxn = 10010;
struct Book{
int id, pubYear;
string title, author, publisher;
vector<string> key;
}books[maxn];
map<int, string> mp;
set<int> idSet;
void print() {
if(idSet.size()==0) {
cout << "Not Found" << endl;
return ;
}
int setSize = idSet.size();
for(auto it=idSet.begin(); it!=idSet.end();it++) {
printf("%07d\n", *it); // ---------------要设定格式符为 07d
}
idSet.clear();
return ;
}
int main() {
int n;
cin >> n;
int id, pubYear;
string title, author, publisher, temp;
string key[5];
for(int i=0; i<n; i++) {
cin >> id;
cin.ignore();
getline(cin, title);
getline(cin, author);
getline(cin, temp);
getline(cin, publisher);
cin >> pubYear;
books[i].id = id;
books[i].title = title;
books[i].author = author;
string t;
int len = temp.length(), j = 0;
while(j < len) {
if(temp[j] != ' ') t += temp[j];
else{
books[i].key.push_back(t);
t = "";
}
if(temp[j] != ' ' && j == len - 1) books[i].key.push_back(t);
j++;
}
books[i].publisher = publisher;
books[i].pubYear = pubYear;
}
mp[1] = "title";
mp[2] = "author";
mp[3] = "key";
mp[4] = "publisher";
mp[5] = "pubYear";
int query, stripe;
string qContent;
cin >> query;
for(int i=0; i<query; i++) {
scanf("%d: ", &stripe);
getline(cin, qContent);
if(mp[stripe] == "title") {
cout << stripe << ": " << qContent << endl;
for(int i=0; i<n; i++) {
if(books[i].title == qContent) {
idSet.insert(books[i].id);
}
}
} else if(mp[stripe] == "author") {
cout << stripe << ": " << qContent << endl;
for(int i=0; i<n; i++) {
if(books[i].author == qContent) {
idSet.insert(books[i].id);
}
}
} else if(mp[stripe] == "key") {
cout << stripe << ": " << qContent << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<books[i].key.size(); j++){
if(books[i].key[j] == qContent) {
idSet.insert(books[i].id);
break;
}
}
}
} else if(mp[stripe] == "publisher") {
cout << stripe << ": " << qContent << endl;
for(int i=0; i<n; i++) {
if(books[i].publisher == qContent) {
idSet.insert(books[i].id);
}
}
} else if(mp[stripe] == "pubYear") {
cout << stripe << ": " << qContent << endl;
for(int i=0; i<n; i++) {
if(books[i].pubYear == stoi(qContent)) {
idSet.insert(books[i].id);
}
}
}
print();
}
return 0;
}