PAT甲级1023,1024解题报告

1023 Have Fun with Numbers (20 分)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

Sample Input:

1234567899

Sample Output:

Yes
2469135798

题目大意:给你一个数,把它乘以2之后得到的数是否是原来的数的一个排列组合。

解题思路:很水,10的20次方的数,肯定不能正常乘以2了,用数组存一下各个位,模拟一下乘法,然后把得到的结果和原来的排序,如果一样就是yes如果不一样就是no。

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
using namespace std;
vector<int> GetDouble(vector<int> cur) {
	vector<int> res;
	int a = 0;//进位
	for (int i = cur.size() - 1; i >= 0; i--) {
		if (i != 0) {
			int temp = cur[i] * 2 + a;
			a = temp / 10;
			res.push_back(temp % 10);
		}
		else {
			int temp = cur[i] * 2 + a;
			a = temp / 10;
			res.push_back(temp % 10);
			if (a != 0)
				res.push_back(a);
		}
	}
	reverse(res.begin(), res.end());
	return res;
}
int main()
{
	string cur;
	cin >> cur;
	vector<int> curvector;
	vector<int> inordercur;
	for (int i = 0; i < cur.size(); i++) {
		curvector.push_back(cur[i] - '0');
		inordercur.push_back(cur[i] - '0');
	}
	sort(inordercur.begin(), inordercur.end());
	vector<int> doublecur = GetDouble(curvector);
	vector<int> inorderdoublecur = doublecur;
	if (doublecur.size() != inordercur.size()) {
		cout << "No" << endl;
		for (int i = 0; i < doublecur.size(); i++) {
			cout << doublecur[i];
		}
		cout << endl;
	}
	else {
		bool flag=true;
		sort(inorderdoublecur.begin(), inorderdoublecur.end());
		for (int i = 0; i < inordercur.size(); i++) {
			if (inordercur[i] != inorderdoublecur[i]) {
				flag = false;
				break;
			}
		}
		if (!flag) {
			cout << "No" << endl;
		}
		else {
			cout << "Yes" << endl;
		}
		for (int i = 0; i < doublecur.size(); i++) {
			cout << doublecur[i];
		}
		cout << endl;
	}
	return 0;
}

1024 Palindromic Number (25 分)

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.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤10​10​​) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

题目大意:把一个数反过来和本身相加得到数,问经过多少步得到一个回文数。还有个步数限制,如果步数限制超出没得到就不需要继续直接输出最后的数就行了。

解题思路:很水,模拟一下大数加法就行了。

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
using namespace std;
bool judge(vector<int> cur) {
	bool flag = true;
	if (cur.size() == 1)flag = true;
	for (int i = 0; i < cur.size() / 2; i++) {
		if (cur[i] != cur[cur.size() - i - 1]) {
			flag = false;
			break;
		}
	}
	return flag;
}
vector<int> Onestep(vector<int> cur) {
	vector<int> temp = cur;
	vector<int> res;
	reverse(temp.begin(), temp.end());
	int a = 0;//进位
	for (int i = cur.size()-1; i >=0; i--) {
		if (i != 0) {
			int t = cur[i] + temp[i] + a;
			a = t / 10;
			res.push_back(t % 10);
		}
		else {
			int t = cur[i] + temp[i] + a;
			a = t / 10;
			res.push_back(t % 10);
			if (a != 0)
				res.push_back(a);
		}
	}
	reverse(res.begin(), res.end());
	return res;
}
int main()
{
	string num;
	int k;
	cin >> num >> k;
	int ans=0;
	vector<int> n;
	for (int i = 0; i < num.size(); i++) {
		n.push_back(num[i] - '0');
	}
	for (int i = 0; i < k; i++) {
		if (judge(n)) {
			break;
		}
		else {
		n = Onestep(n);
		ans++;
		}	
	}
	for (int i = 0; i < n.size(); i++) {
		cout << n[i];
	}
	cout << endl;
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/TateBrwonJava/article/details/82966469