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

 

       最後還是推薦你們去看柳神的代碼吧,雖然我沒有仔細看過,但是瞟了一眼代碼是真的簡練,我的代碼和柳神的一比簡直就是又臭又長。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章