PAT (Advanced Level)-1024. Palindromic Number (25)

PAT (Advanced Level)-1024. Palindromic Number (25)

1024. Palindromic Number (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue


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 (<= 1010) 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

解读

这一天考查的很简单,就是如何判断回文数。但是按照题目的条件,即便使用long long类型都无法全部通过检查点。故而遇见此类大数问题可以使用string来避免上限的限制。 熟练掌握字符和数值的转换。可以提升写代码的速度。同样也可以用STL标准库函数来缩减代码量。

完整代码

#include<iostream>
#include<cstring>
#include<string>


using namespace std;

string add(string str1, string str2)
{
	if (str2.length() > str1.length())
	{
		string buf;
		for (int i = 0; i < (str2.length() - str1.length()); i++)
		{
			buf += "0";
		}
		buf += str1;
		str1 = buf;
	}
	else
	{
		string buf;
		for (int i = 0; i < (str1.length() - str2.length()); i++)
		{
			buf += "0";
		}
		buf += str2;
		str2 = buf;
	}

	string result;
	bool addition = false;
	for (int i = str1.length() - 1; i >= 0; i--)
	{
		int val = (str1[i] - '0') + (str2[i] - '0') + addition;
		if (val >= 10)
		{
			result += (val - 10 + '0');
			addition = true;
		}
		else
		{
			result += (val + '0');
			addition = false;
		}
	}
	if (addition)
	{
		result += '1';
	}

	string RealResult;
	for (int i = 0; i < result.length(); i++)
	{
		RealResult += result[result.length() - 1 - i];
	}

	return RealResult;
}

string inverted_sequence(string N)// 翻转数字
{
	string rN;

	for (int i = 0; i < N.length(); i++)
	{
		rN += N[N.length() - 1 - i];
	}
	
	return rN;
}

bool J_Palindromic(string Palindromic)//判断回文
{
	int L = Palindromic.length();
	int signBOOL=0;
	for (int i = L-1; i > ((L-1) / 2); i--)
	{
		if (Palindromic[i] != Palindromic[L - 1 - i])
			signBOOL = 1;
	}
	if (signBOOL == 1)
		return false;
	else
		return true;

}

int main()
{
	string N;
	int K;
	cin >> N >> K;

	if (J_Palindromic(N))//判断输入的数字是否为回文,是则输出。
	{
		cout << N << '\n' << 0;
	}
	else 
	{
		string rN;
		bool signBOOL;//回文标记。

		for (int i = 1; i <= K; i++)
		{
			rN = inverted_sequence(N);
			N = add(N, rN);
			if (signBOOL = J_Palindromic(N))
			{
				cout << N << '\n' << i;
				break;
			}
			if (i == K)
			{
				cout << N << '\n' << K;
			}
		}

	}


	return 0;
}