【一隻蒟蒻的刷題歷程】 【PAT】 A1024 迴文數

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


題目大意:

向前或向後寫入時將相同的數字稱爲迴文數。例如,1234321是迴文數。所有一位數字都是迴文數。

非迴文數可以通過一系列操作與迴文數配對。首先,將非迴文數反轉,並將結果添加到原始數中。如果結果不是迴文數,則重複此操作直到給出迴文數。例如,如果我們從67開始,我們可以分兩步獲得迴文數:67 + 76 = 143,以及143 + 341 = 484。

給定任何正整數N,您應該找到它的配對迴文數和找到它的步驟數。

輸入規格:

每個輸入文件包含一個測試用例。每種情況都由兩個正數N和K組成,其中N(≤1010 10)是初始數字,K(≤100)是最大步數。這些數字用空格分隔。

輸出規格:

對於每個測試用例,輸出兩個數字,每行一個。第一個數字是配對的迴文數N,第二個數字是查找回文數所採取的步驟數。如果在K步之後未找到迴文數,則只需輸出在K步獲得的數,然後輸出K。

樣本輸入1:

67 3

樣本輸出1:

484
2

樣本輸入2:

69 3

樣本輸出2:

1353
3


思路:

第一次沒看出來數很大,所以就普通模擬了一遍,結果只對了四個測試點,後來發現了數字很大,用了高精度加法,輸入時n用int輸入,然後to_string轉字符串,最後兩個測試點怎麼都過不了。。。檢查了好幾個小時,後來直接把n定義爲string就過了。。。


代碼:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
string add(string x) //逆序 並且相加
{
	string ans;
	string s = x;
	reverse(s.begin(),s.end()); //s爲逆序後的數,x原來的數
	
	int carry=0;   //進位
	for(int i=s.size()-1;i>=0;i--) //高精度加法
	{
		int sum = s[i]-'0'+x[i]-'0'+carry;
		ans += sum%10+'0';
		carry = sum/10;
	}
	if(carry) ans+=carry+'0';
	reverse(ans.begin(),ans.end()); //最後還要倒置一遍
	return ans;
}

int main() 
{
   string s;
    /*剛開始定義成int,然後用to_string轉成字符串,
    最後兩個測試點一直過不了(現在也不知道爲什麼),
    不知道爲什麼,直接改成string就可以了*/
   int k;
   cin>>s>>k;
   string temp=s;
   reverse(temp.begin(),temp.end());
   //如果一開始本身就是迴文,那就直接輸出
   if(temp==s) cout<<s<<endl<<0; 
   else
   {
   	   int now =0 ;
   	   while(now<k) 
   	   {
   	     now++;
   	   	 s = add(s); //倒序並相加
   	     temp = s;
   	     reverse(temp.begin(),temp.end());
   	     if(temp == s) break; //if迴文 結束循環
	   }
   	   cout<<s<<endl<<now;
   }
   
    return 0;
}

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