坑点
如果查询输入的年份用int类型存储,输出时要用%04d,查询中的年份可能小于1000
#include<bits/stdc++.h>
using namespace std;
struct Book{
int id;
string title,author;
vector<string> key;
string pub;
int year;
};
bool cmp(Book a,Book b)
{
return a.id<b.id;
}
int main()
{
int n,m;
string k;
char c;
scanf("%d",&n);
Book book[n];
for(int i=0;i<n;i++)
{
scanf("%d",&book[i].id);
getchar();
getline(cin,book[i].title);
getline(cin,book[i].author);
do{
cin>>k;
book[i].key.push_back(k);
c=getchar();
}while(c!='\n');
getline(cin,book[i].pub);
scanf("%d",&book[i].year);
}
sort(book,book+n,cmp);
scanf("%d",&m);
int num,y;string s;
for(int i=0;i<m;i++)
{
scanf("%d: ",&num);
int flag=0;
if(num!=5){
getline(cin,s);
cout<<num<<": "<<s<<endl;
if(num==1){
for(int j=0;j<n;j++)
{
if(book[j].title==s){
printf("%07d\n",book[j].id);
flag=1;
}
}
}
else if(num==2){
for(int j=0;j<n;j++)
{
if(book[j].author==s){
printf("%07d\n",book[j].id);
flag=1;
}
}
}
else if(num==3){
for(int j=0;j<n;j++)
{
for(int k=0;k<book[j].key.size();k++)
{
if(book[j].key[k]==s)
{
printf("%07d\n",book[j].id);
flag=1;break;
}
}
}
}
else{
for(int j=0;j<n;j++)
{
if(book[j].pub==s){
printf("%07d\n",book[j].id);
flag=1;
}
}
}
}
else{
scanf("%d",&y);
printf("%d: %04d\n",num,y);
for(int j=0;j<n;j++)
{
if(book[j].year==y){
printf("%07d\n",book[j].id);
flag=1;
}
}
}
if(!flag) printf("Not Found\n");
}
return 0;
}