题目

题意: 给定图书信息,对于m个查询给出图书书号

tip:模拟+sort

#include<iostream>
#include<cstring> 
#include<algorithm>
using namespace std;
struct ss {
	string id,title,author,key_word,publisher,pub_year;
};
bool cmp(struct ss a,struct ss b) {
	return a.id<b.id;
}
int main() {
	int n;
	cin>>n;
	getchar();
	struct ss books[n];
	for(int i=0; i<n; ++i) {
		getline(cin,books[i].id);
		getline(cin,books[i].title);
		getline(cin,books[i].author);
		getline(cin,books[i].key_word);
		getline(cin,books[i].publisher);
		getline(cin,books[i].pub_year);
	}
	sort(books,books+n,cmp);
	int m;
	cin>>m;
	getchar();
	for(int i=0; i<m; ++i) {
		string query;
		getline(cin,query);
		cout<<query<<endl;
		char a=query[0];
		query=query.substr(3,query.size()-3);
		int flag=0;
		if(a=='1') {
			for(int j=0; j<n; ++j)
				if(books[j].title==query) {
					cout<<books[j].id<<endl;
					flag=1;
				}
			if(!flag)
				cout<<"Not Found\n";
		} else if(a=='2') {
			for(int j=0; j<n; ++j)
				if(books[j].author==query) {
					cout<<books[j].id<<endl;
					flag=1;
				}
			if(!flag)
				cout<<"Not Found\n";
		} else if(a=='3') {
			for(int j=0; j<n; ++j)
				if(strstr(books[j].key_word.c_str(),query.c_str())) {
					cout<<books[j].id<<endl;
					flag=1;
				}
			if(!flag)
				cout<<"Not Found\n";
		} else if(a=='4') {
			for(int j=0; j<n; ++j)
				if(books[j].publisher==query) {
					cout<<books[j].id<<endl;
					flag=1;
				}
			if(!flag)
				cout<<"Not Found\n";
		} else {
			for(int j=0; j<n; ++j)
				if(books[j].pub_year==query) {
					cout<<books[j].id<<endl;
					flag=1;
				}
			if(!flag)
				cout<<"Not Found\n";
		}
	}
	return 0;
}