PAT-1022 Digital Library (30)

题目大意:给定 N 本书的信息(书号、书名 [1]、作者 [2]、书中关键字 [3]、出版社名 [4]、出版年月 [5],编号已注明),每次查询编号的内容,输出有相同内容书的编号;不存在则输出“Not Found”。

解题思路:本题的一大考点是对数据输入的控制;我用 map<string, set<int > > 对数据进行存储,即 string 类型的数据(即查找内容)对应一个集合 set<int > 其内存储书的编号;其余部分需要注意的是如果用 int 存储书的编号,要以 “%07d” 的形式输出哦。

题目链接:https://www.patest.cn/contests/pat-a-practise/1022

#include <iostream>              
#include <algorithm>              
#include <set>              
#include <map>              
#include <vector>              
#include <stack>              
#include <queue>              
#include <cmath>              
using namespace std;

map<string, set<int > > MM[6];

int main(int argc, char** argv) {
	int n;
	cin >> n;
	for(int i=0;i<n;++i)
	{
		int num,j=0;
		cin >> num;
		char s,author[85],title[85],keyword[15],press[85],data[5];
		getchar(); 
		//title
		title[j] = getchar();
		while(title[j] != '\n')
		{
			title[++j] = getchar();
		}
		title[j] = '\0';
		MM[1][title].insert(num);
		//name
		j = 0;
		author[j] = getchar();
		while(author[j] != '\n')
		{
			author[++j] = getchar();
		}
		author[j] = '\0';
		MM[2][author].insert(num);
		//keyword
		j = 0;
		keyword[j] = getchar();
		while(keyword[j] != '\n')
		{
			if(keyword[j] == ' ')
			{
				keyword[j] = '\0';
				if(j >= 1)
					MM[3][keyword].insert(num);
				j=-1; 
			}
			keyword[++j] = getchar();
		}
		keyword[j] = '\0';
		MM[3][keyword].insert(num);
		//press
		j = 0;
		press[j] = getchar();
		while(press[j] != '\n')
		{
			press[++j] = getchar();
		}
		press[j] = '\0';
		MM[4][press].insert(num);
		
	    //data
		j = 0;
		data[j] = getchar();
		while(data[j] != '\n')
		{
			data[++j] = getchar();
		}
		data[j] = '\0';
		MM[5][data].insert(num);
	} 
	int m;
	cin >> m;
	char str[85];
	for(int i=0;i<m;++i) 
	{
		int num;
		scanf("%d: ",&num);
		gets(str);
		cout << num << ": "<< str << endl;
		if(MM[num][str].size() == 0)
			cout << "Not Found" << endl;
		for(set<int >::iterator it=MM[num][str].begin();it!=MM[num][str].end();++it)
		{
			printf("%07d\n",*it);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值