PAT (Advanced Level) Practice 1136 A Delayed Palindrome (20 分) 凌宸1642
题目描述:
Consider a positive integer N written in standard notation with k+1 digits aias ak ⋯a1 a0 with 0 ≤ ai < 10 for all i and ak>0. Then N is palindromic if and only if ai = ak−i for all i. Zero is written 0 and is also palindromic by definition.
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. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, you are supposed to find its paired palindromic number.
译:考虑一个用标准符号写成的正整数 N,其中 k+1 个数字 ai 为 ak ⋯a1 a0 0 ≤ ai < 10 对于所有 i 和 ak >0。 那么 N 是回文的当且仅当 ai =ak−i 对于所有 i。 零写为 0,根据定义也是回文。
非回文数可以通过一系列操作与回文数配对。 首先将非回文数取反,将结果加到原数上。 如果结果不是回文数,则重复此操作直到给出回文数。 这样的数字称为延迟回文数。 (引自 https://en.wikipedia.org/wiki/Palindromic_number )
给定任何正整数,你应该找到它的成对回文数。
Input Specification (输入说明):
Each input file contains one test case which gives a positive integer no more than 1000 digits.
译:每个输入文件包含一个测试用例,它给出一个不超过 1000 位的正整数。
output Specification (输出说明):
对于每个测试用例,逐行打印查找回文数的过程。 每行的格式如下:
A + B = C
where A
is the original number, B
is the reversed A
, and C
is their sum. A
starts being the input number, and this process ends until C
becomes a palindromic number – in this case we print in the last line C is a palindromic number.
; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations.
instead.
译:For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:
A + B = C
其中 A
是原始数,B
是反转的 A
,C
是它们的总和。 A
开始是输入数字,这个过程结束直到 C
变成一个回文数——在这种情况下,我们在最后一行打印C is a palindromic number.
。 或者如果在 10 次迭代中找不到回文数,则打印 Not found in 10 iterations.
。
Sample Input1 (样例输入1):
97152
Sample Output1 (样例输出1):
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
Sample Input2 (样例输入2):
196
Sample Output2 (样例输出2):
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.
The Idea:
- 回文数的判断。
- 大整数的加法。
The Codes:
#include<bits/stdc++.h>
using namespace std ;
int n = 10 ;
string a , b , c ;
string add(string a , string b){ // 得到的是 a + b 的结果的字符串的逆序
string res = "" ;
int temp = 0 ; // 存储对应位置上的相加的结果以及进位
for(int i = a.size() - 1 ; i >= 0 ; i --){
temp += (a[i] - '0') + (b[i] - '0') ;
res += (temp % 10) + '0' ;
temp /= 10 ;
}
if(temp) res += temp + '0' ;
return res ;
}
int main(){
cin >> a ;
b = a ;
reverse(b.begin() , b.end()) ;
while(n -- > 0){ // 如果 10 次操作之后,还没找到的话,则 n 会变为 -1
if(a == b) {
cout << a << " is a palindromic number."<<endl ;
break ;
}
cout << a << " + " << b << " = " ;
c = add(a , b) ;
b = c ;
reverse(c.begin() , c.end()) ; // 逆序之后得到正确的 a + b 的结果
cout << c << endl ;
a = c ;
}
if(n == -1) cout << "Not found in 10 iterations." << endl ;
return 0 ;
}