PAT甲级1019,1022解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

1019 General Palindromic Number (20 分)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N>0 in base b≥2, where it is written in standard notation with k+1 digits a​i​​ as ∑​i=0​k​​(a​i​​b​i​​). Here, as usual, 0≤a​i​​<b for all i and a​k​​ is non-zero. Then N is palindromic if and only if a​i​​=a​k−i​​ for all i. Zero is written 0 in any base and is also palindromic by definition.

Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and b, where 0<N≤10​9​​ is the decimal number and 2≤b≤10​9​​ is the base. The numbers are separated by a space.

Output Specification:

For each test case, first print in one line Yes if N is a palindromic number in base b, or No if not. Then in the next line, print N as the number in base b in the form "a​k​​ a​k−1​​ ... a​0​​". Notice that there must be no extra space at the end of output.

Sample Input 1:

27 2

Sample Output 1:

Yes
1 1 0 1 1

Sample Input 2:

121 5

Sample Output 2:

No
4 4 1

题目大意:给一个数和进制数,判断在这个进制下的数是否是回文串。即正着倒着都一样。

解题思路:水题。就有一个特殊情况0,0在任何进制下都是0,这点题目特意指出了,所以没什么坑的了。就是用数组存一下这个数进制转换后的数,然后反一下比较一下是否一样就行了。

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
using namespace std;
vector<int> GetArr(int N, int b) {
	vector<int> res;
	while (N) {
		res.push_back(N%b);
		N = N / b;
	}
	reverse(res.begin(), res.end());
	return res;
}
int main()
{
	int N, b;
	cin >> N >> b;
	vector<int> cur = GetArr(N, b);
	if (N == 0) {
		cout << "Yes" << endl;
		cout << 0 << endl;
	}
	else {
		bool flag = true;
		vector<int> resecur;
		for (int i = cur.size()-1; i>=0; i--) {
			resecur.push_back(cur[i]);
		}
		for (int i = 0; i<cur.size(); i++) {
			if (cur[i] != resecur[i]) {
				flag = false;
				break;
			}
		}
		if (flag)cout << "Yes" << endl;
		else
			cout << "No" << endl;
		for (auto i = cur.begin(); i != cur.end(); i++) {
			if (i != cur.end() - 1)
				cout << (*i) << " ";
			else
				cout << (*i) << endl;
		}
	}
	return 0;
}

1022 Digital Library (30 分)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​4​​) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

  • Line #1: the 7-digit ID number;
  • Line #2: the book title -- a string of no more than 80 characters;
  • Line #3: the author -- a string of no more than 80 characters;
  • Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  • Line #5: the publisher -- a string of no more than 80 characters;
  • Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (≤1000) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:

  • 1: a book title
  • 2: name of an author
  • 3: a key word
  • 4: name of a publisher
  • 5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print Not Found instead.

Sample Input:

3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla

Sample Output:

1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found

题目大意:模拟一下图书馆查询系统,给你书本的id,作者,关键词等一系列信息,要实现根据不同信息筛选出符合条件的所有书本id。

解题思路:比较高效的应该是用map去实现查询,但这题数据要求不高,也是可以正常手模的。暴力手摸也是可以过的。需要注意的是没有书的情况,还有是读入样例数的两次注意吃一下回车或者用scanf读也行。因为后面是要一行一行读的,如果回车没吃掉会少一行的,然后就是关键词的处理,可以直接一行读进来用按空格分割一下。C++应该没有提供这方面的库函数自己写一下,或者你直接在读的时候用字符串数组读一下,因为练习,我还是练习一下字符串操作手摸了一下。

比较坑的一点,输出的时候,把输入的查询语句也输出一下,不然一个都不会对的。

代码如下

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
using namespace std;
struct book {
	string id;
	string title;
	string author;
	vector<string> keywords;
	string publisher;
	string year;
};
vector<string> GetStringArr(string a) {
	vector<string> res;
	int startpos = 0;
	for (int i = 0; i < a.size(); i++) {
		if (a[i] == ' ') {
			res.push_back(a.substr(startpos, i - startpos));
			startpos = i + 1;
		}
	}
	res.push_back(a.substr(startpos, a.size() - startpos));
	return res;
}
bool Cmp(book a, book b) {
	return a.id < b.id;
}
book list[10005];
int main()
{
	int n;
	cin >> n;
	getchar();
	if (n <= 0) {
		int q;
		cin >> q;
		while (q--) {
			string tmp;
			cin >> tmp;
			cout << "Not Found" << endl;
		}
	}
	for (int i = 0; i < n; i++) {
		getline(cin, list[i].id);
		getline(cin, list[i].title);
		getline(cin, list[i].author);
		string key;
		getline(cin, key);
		list[i].keywords = GetStringArr(key);
		//for (int j = 0; j < list[i].keywords.size(); j++)cout << list[i].keywords[j] << endl;
		getline(cin, list[i].publisher);
		getline(cin, list[i].year);
	}
	sort(list, list + n, Cmp);
	int m;
	cin >> m;
	getchar();
	while (m--) {
		string cur;
		string waitfind;
		getline(cin, cur);
		bool flag;
		waitfind = cur.substr(3, cur.size() - 3);
		cout << cur << endl;
		switch (cur[0])
		{
		case '1':
			flag = false;
			for (int i = 0; i < n; i++) {
				if (list[i].title == waitfind) {
					cout << list[i].id << endl;
					flag = true;
				}
			}
			if (!flag)cout << "Not Found" << endl;
			break;
		case '2':
			flag = false;
			for (int i = 0; i < n; i++) {
				if (list[i].author == waitfind) {
					cout << list[i].id << endl;
					flag = true;
				}
			}
			if (!flag)cout << "Not Found" << endl;
			break;
		case '3':
			flag = false;
			for (int i = 0; i < n; i++) {
				if (find(list[i].keywords.begin(), list[i].keywords.end(), waitfind) != list[i].keywords.end()) {
					flag = true;
					cout << list[i].id << endl;
				}
			}
			if (!flag)cout << "Not Found" << endl;
			break;
		case '4':
			flag = false;
			for (int i = 0; i < n; i++) {
				if (list[i].publisher == waitfind) {
					cout << list[i].id << endl;
					flag = true;
				}
			}
			if (!flag)cout << "Not Found" << endl;
			break;
		case '5':
			flag = false;
			for (int i = 0; i < n; i++) {
				if (list[i].year == waitfind) {
					cout << list[i].id << endl;
					flag = true;
				}
			}
			if (!flag)cout << "Not Found" << endl;
			break;
		default:
			break;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值