PAT甲级1024

       

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

       说来也巧,今天刚好ac了一道pat甲级的题目,编号是1024,本来没什么,忽然意识到今天刚好也是1024程序员节,那就分享一下这道题吧。

        照例,我先解释一下题目,题目的意思是说给我们一个很大的数字,让我们检查是否是回文数,如果是回文数则直接输出,如果不是回文数则将这个数倒置并与原数相加,再检查是否是回文数,反复执行上述操作,直至检查结果为是回文数或者达到了操作次数上限,此时,无论该数是否为回文数都输出该数,并输出此时操作的次数。

       这题的思路其实并不复杂,无非就是对回文数的检测和对大数的叠加,我是采用了栈作为存储结构来完成对上述操作进行支持,使用栈说实话对于回文数的检测其实是一种非常好的思路,就是先把这个大数装入一个栈A中,再把这个栈A中的数据依次弹栈,再把数据压如栈B,则此时栈A和栈B中的顺序是刚好相反的,此时对两个栈中的数据依次进行对比即可,就完成了回文数的首尾比较。这个思路非常好理解,但是代码量可能会稍微多一点。

       而对两个大数的相加我也是使用的栈作为数据结构,考虑主要是它方便扩充容量和对其中的数据进行操作。但是由于我之前没有对这方面的思考,导致代码和思路不是特别的优美。我的思路如下:开始先把数字0压入结果栈C,然后从A栈和B栈分别取栈顶元素,将栈A,栈B和栈C的栈顶元素相加并设结果为X,如果该结果大于9,则说明有进位,先对栈C进行弹栈操作,然后把X的个位压入栈C,再压入1作为相加的进位,若结果小于等于9,操作大致一样,只是最后压入0作为进位。当该相加操作完成后,结果相当于原大数和原大数的倒置相加,再倒置放入栈中。(这个是我自己硬想出来的,所以可能比较难理解,一句话解释其实就是逐位相加,并把进位提前压入栈中,无论进位是0还是1)

以下是我的代码:

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
	string N;
	int K;
	cin >> N >> K;
	int length = N.size();
	stack<int> s1;
	stack<int> s2;
	stack<int> s3;
	for (int i = 0; i < length; i++)
	{
		s1.push(N[i] - 48);
	}
	for (int i = 0; i < K; i++)
	{
		stack<int> temp1;
		stack<int> temp2;
		temp1 = s1;
		while (!s1.empty())
		{
			s2.push(s1.top());
			s1.pop();
		}
		temp2 = s2;
		s1 = temp1;
		int size=s1.size();
		int count = 0;
		for (int j = 0; j<size; j++)
		{
			if (s1.top()!= s2.top())
			{
				count++;
				break;
			}
			s1.pop();
			s2.pop();
		}
		if (count >0)
		{
			int size = temp1.size();
			s3.push(0);
			for (int j = 0; j < size; j++)
			{
				int x;
				x = s3.top()+temp1.top() + temp2.top();
				if (x>=10)
				{
					s3.pop();
					s3.push(x-10);
					s3.push(1);
				}
				else
				{
					s3.pop();
					s3.push(x);
					s3.push(0);
				}
				temp1.pop();
				temp2.pop();
			}
			if (s3.top() == 0)
			{
				s3.pop();
			}
			s1 = s3;
			while (!s2.empty())
			{
				s2.pop();
			}
			while (!s3.empty())
			{
				s3.pop();
			}
		}
		else if (count == 0)
		{
			int size = temp1.size();
			for (int j = 0; j < size; j++)
			{
				cout << temp1.top();
				temp1.pop();
			}
			cout << endl;
			cout<< i;
			return 0;
		}
	}
	while (!s1.empty())
	{
		cout << s1.top();
		s1.pop();
	}
	cout << endl;
	cout<< K;
	return 0;
}

 

       最后还是推荐你们去看柳神的代码吧,虽然我没有仔细看过,但是瞟了一眼代码是真的简练,我的代码和柳神的一比简直就是又臭又长。

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd3b4ac2b717778ac20b94b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞