标签:using cout ios class author one title int main
https://www.patest.cn/contests/pat-a-practise/1022
直接模拟,
输入,按id排序,检索
#include <iostream> #include <string> #include <algorithm> using namespace std; struct book //图书结构 { string id; string title; string author; int k; //关键词数量 string key[5]; string pub; string year; }; bool cm(const book &b1,const book &b2); //根据id排序 int main() { int n,m,i,j,k; string temp_s; book temp_b,*bks; cin>>n; bks=new book [n]; cin.get(); //字符串输入过滤回车与空格 for(i=0;i<n;i++) //输入书的信息 { getline(cin,bks[i].id); getline(cin,bks[i].title); getline(cin,bks[i].author); getline(cin,temp_s); for(j=0,k=0;j<temp_s.size();j++) { if(temp_s[j]!=‘ ‘) bks[i].key[k]+=temp_s[j]; else k++; } bks[i].k=k+1; getline(cin,bks[i].pub); getline(cin,bks[i].year); } sort(bks,bks+n,cm); //根据id排序 cin>>m; int cl; int flag; //标志是否有找到符合条件的书 for(i=0;i<m;i++) { cin>>cl; cin.get(); //跳过":" cin.get(); //跳过" " flag=0; getline(cin,temp_s); cout<<cl<<": "<<temp_s<<endl; for(j=0;j<n;j++) { switch(cl) { case 1:if(bks[j].title==temp_s) { cout<<bks[j].id<<endl; flag=1; } break; case 2:if(bks[j].author==temp_s) { cout<<bks[j].id<<endl; flag=1; } break; case 3:for(k=0;k<bks[j].k;k++) if(bks[j].key[k]==temp_s) { cout<<bks[j].id<<endl; flag=1; } break; case 4:if(bks[j].pub==temp_s) { cout<<bks[j].id<<endl; flag=1; } break; case 5:if(bks[j].year==temp_s) { cout<<bks[j].id<<endl; flag=1; } break; } } if(flag==0) cout<<"Not Found\n"; } return 0; } bool cm(const book &b1,const book &b2) { return b1.id<b2.id; }
PAT A 1022. Digital Library (30)【结构体排序检索】
标签:using cout ios class author one title int main
原文地址:http://www.cnblogs.com/demian/p/6090353.html