PAT A1022(测试点3,4答案错误)

定义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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值