PAT(甲级)1022笔记??为什么过不了编译

PAT(甲级)1022笔记??为什么过不了编译

1、原题参照PAT官网

2、生词

3、知识点

  • 我一开始是想用struct结构体来实现存储,但答案给的是字典。字典用的不多,从这里开始学习吧。查找属于map映射

  • getline() 用于string类的。使用需包含头文件#include。getline(cin,string s),接收一个字符串,可以接收空格、回车等

    • 与cin.getline()的区别:
      1.cin.getline()接收输入字符串的是数组,getline()是string类型。
      2.cin.getline()可以接收空格,但不能接收回车;getline()可以接收空格和回车
      3.cin.getline()会在数组结尾是’\0’,getline()不会
  • scanf_s 和scanf好麻烦啊,and为什么结果一样都对的就是过不了编译?气死我了。明天继续查。

4、代码record



#include<stdio.h>
#include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<vector>
#include <iomanip>
#include<set>
#include<map>
using namespace std;
#define MAXSIZE 10000


//下面这几个map的意思是先创建一个空的
map<string, set<int>> title, author, keyword, pub, year;
void query(map<string, set<int>>& m, string& str) {
	if (m.find(str) != m.end()) {
		for (auto it = m[str].begin(); it != m[str].end(); it++)
			printf("%07d\n", *it);
		}
	else
		cout << "Not found\n";

}

int main() {
	int N, Cnum, id, num;
	cin >> N;   //N本图书
	string ttitle, tauthor, tkey, tpub, tyear;
	//循环存储数据
	for (int i = 0; i < N; i++)
	{
		//cin >> id;
		scanf_s("%d\n", &id);
		getline(cin, ttitle);//直接获取一整行

		//cin.getline >> ttitle;  //和getline不一样,这个接受的是数组,上面那个是字符串。
		title[ttitle].insert(id);
		getline(cin, tauthor);
		author[tauthor].insert(id);
		//循环接受变量,因为不知道有几个关键词。
		while (cin >> tkey) {
			keyword[tkey].insert(id);
			char c = getchar();
			if (c =='\n')break;

		}
		getline(cin, tpub);
		//cin.getline >> tpub;
		pub[tpub].insert(id);
		getline(cin, tyear);
		//cin.getline >> tyear;
		year[tyear].insert(id);
	}
	//输入查找数据
	cin >> Cnum;
	for (int j = 0; j < Cnum; j++)
	{
		scanf_s("%d", &num);
		//接受没用的“: ”
		char mao=getchar(), kong=getchar();

		string temp;
		getline(cin, temp);
		//cin.getline >> temp;
		//查找并输出结果
		cout << num << ": " << temp << endl;
		if (num == 1)query(title, temp);
		else if (num == 2)query(author, temp);
		else if (num == 3)query(keyword, temp);
		else if (num == 4)query(pub, temp);
		else if (num == 5)query(year, temp);
	}

	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值