1. 程式人生 > >PAT甲級1023,1024解題報告

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