【PAT甲级】1022 Digital Library (30 分)(模拟)

题意:

输入一个正整数N(<=10000),接下来输入N组数据,ID,书名,作者,关键词,出版社,出版年份。

然后输入一个正整数M(<=1000),接下来输入查询的数据,递增输出ID,若没有查找到则输出Not Found。

trick:

第三组数据中会有需要补0的ID,建议采用printf("%07d",ID)输出。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
typedef struct book{
int ID;
char name[107];
char writer[107];
char words[7][107];
char publisher[107];
char year[107];
int length[7];
int wordlength[7];
};
book b[10007];
char s[1007];
bool cmp(book x,book y){
if(x.ID!=y.ID)
return x.ID<y.ID;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&b[i].ID);
char x=0;
int tt=0;
int tot=0;
getchar();
while(1){
scanf("%c",&x);
if(x=='\n')
break;
b[i].name[tot++]=x;
}
b[i].length[1]=tot;
x=0;
tt=0;
tot=0;
while(1){
scanf("%c",&x);
if(x=='\n')
break;
b[i].writer[tot++]=x;
}
b[i].length[2]=tot;
x=0;
tt=0;
tot=0;
while(1){
scanf("%c",&x);
if(x=='\n')
break;
else if(x==' '){
b[i].wordlength[tot]=tt;
++tot;
tt=0;
}
else
b[i].words[tot][tt++]=x;
}
b[i].wordlength[tot]=tt;
b[i].length[3]=tot;
tot=0;
x=0;
tt=0;
while(1){
scanf("%c",&x);
if(x=='\n')
break;
b[i].publisher[tot++]=x;
}
b[i].length[4]=tot;
x=0;
tt=0;
tot=0;
while(1){
scanf("%c",&x);
if(x=='\n')
break;
b[i].year[tot++]=x;
}
b[i].length[5]=tot;
}
sort(b+1,b+1+n,cmp);
/*
for(int i=1;i<=n;++i){
cout<<b[i].ID<<"\n";
cout<<b[i].name<<"\n";
cout<<b[i].writer<<"\n";
for(int j=0;j<=b[i].length[3];++j)
cout<<b[i].words[j]<<"\n";
cout<<b[i].publisher<<"\n";
cout<<b[i].year<<"\n";
for(int j=1;j<5;++j)
cout<<b[i].length[j]<<"\n";
}
*/
int m;
scanf("%d",&m);
for(int i=1;i<=m;++i){
memset(s,0,sizeof(s));
if(i==1)
getchar();
int cnt=0;
char xx=0;
int tot=0;
while(1){
scanf("%c",&xx);
if(xx=='\n')
break;
s[tot++]=xx;
}
printf("%s\n",s);
int len=tot;
int x=s[0]-'0';
for(int j=1;j<=n;++j){
int flag=0;
if(len<3)
continue;
if(x==1){
if(len-3==b[j].length[1]){
for(int l=3;l<len;++l){
if(b[j].name[l-3]!=s[l]){
flag=1;
break;
}
}
}
else
flag=1;
}
else if(x==2){
if(len-3==b[j].length[2]){
for(int l=3;l<len;++l){
if(b[j].writer[l-3]!=s[l]){
flag=1;
break;
}
}
}
else
flag=1;
}
else if(x==3){
for(int l=0;l<=b[j].length[3];++l){
if(len-3==b[j].wordlength[l]){
for(int h=3;h<len;++h){
if(b[j].words[l][h-3]!=s[h]){
flag++;
break;
}
}
}
else
flag++;
}
}
else if(x==4){
if(len-3==b[j].length[4]){
for(int l=3;l<len;++l){
if(b[j].publisher[l-3]!=s[l]){
flag=1;
break;
}
}
}
else
flag=1;
}
else if(x==5){
if(len-3==b[j].length[5]){
for(int l=3;l<len;++l){
if(b[j].year[l-3]!=s[l]){
flag=1;
break;
}
}
}
else
flag=1;
}
if(x==3&&flag<b[j].length[3]+1||x!=3&&!flag){
++cnt;
printf("%07d\n",b[j].ID);
}
}
if(!cnt)
printf("Not Found\n");
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11434970.html