1136 A Delayed Palindrome
Consider a positive integer N written in standard notation with k+1 digits a i a_ia
i
as a k ⋯ a 1 a 0 a_k\ ⋯\ a_1\ a_0a
k
⋯ a
1
a
0
with 0 ≤ a i < 10 0≤a_i<100≤a
i
<10 for all i and a k > 0 a_k>0a
k
>0. Then N is palindromic if and only if a i = a k − i a_i=a_k−ia
i
=a
k
−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.
Input Specification:
Each input file contains one test case which gives a positive integer no more than 1000 digits.:https://blog.csdn.net/Newin2020/article/details/126754745
Output Specification:
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
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.
Sample Input 1:
97152 • 1
Sample Output 1:
97152 + 25179 = 122331 122331 + 133221 = 255552 255552 is a palindromic number.
Sample Input 2:
196 • 1
Sample Output 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.
题意
这道题是 PAT 1024 那道题的改编,几乎一样。同样想要去获得一个回文数,例如 1234321 。
如果给定的数字不是回文数则需要将该数字加上它的逆序数,例如 123 不是回文数,则要加上它的逆序数 321 。然后再判断加上逆序数之后的数是否为回文数,如果还不是就重复上述操作直至是回文数为止。
但是题目有个限制,上述的加法操作最多只能进行十次,如果十次后还不是回文数就不管了,并且输出 Not found in 10 iterations. ,否则输出 i is a palindromic number. ,其中 i 是最终得到的回文数。
思路
这道题给定的数字很大,如果正常加法会爆 int ,需要用到高精度操作,所以一开始我们用字符串类型读入数字,然后再将字符串中的每一位都放进数组当中,注意要从字符转化回数字即 n[i] - '0' 。另外,我们数组中下标为 0 的位置存的是数字的最高位,例如 12345 放到数组中,下标从 0 到 4 存的是从 5 到 1 的数,即倒着放进数组,这样有利于我们后续的高精度操作。
先判断该数字是否为回文数,如果不是就进行上述的加法操作。另外,我们将数字反转利用了 c++ 的语法。
这里套用了高精度的加法模板,其中有些地方可以省略,因为这道题加法的两个数长度是相同的,所以可以不用同时判断 i 小于 a 和 b 的长度。另外,因为 t 是可能会有进位,所以需要判断 t 是否为 0 ,如果不为 0 还需要加到答案数组中。
在操作的过程中需要输出操作结果,格式为 A + B = C 。
根据最后一个数是否是回文数,输出对应的结果。
代码
#include<bits/stdc++.h> using namespace std; //判断是否为回文数 bool check(vector<int> a) { for (int i = 0, j = a.size() - 1; i < j; i++, j--) if (a[i] != a[j]) return false; return true; } //高精度加法模板 vector<int> add(vector<int> a, vector<int> b) { vector<int> c; for (int i = 0, t = 0; i < a.size() || i < b.size() || t; i++) { if (i < a.size()) t += a[i]; if (i < b.size()) t += b[i]; c.push_back(t % 10); t /= 10; } return c; } //打印数字 void print(vector<int> a) { for (int i = a.size() - 1; i >= 0; i--) cout << a[i]; } int main() { string n; cin >> n; vector<int> a; for (int i = n.size() - 1; i >= 0; i--) a.push_back(n[i] - '0'); for (int i = 0; i < 10; i++) { if (check(a)) break; //先判断是否为回文数 vector<int> b(a.rbegin(), a.rend()); //将数字逆转 print(a), cout << " + ", print(b), cout << " = "; a = add(a, b); //执行高精度加法操作 print(a), cout << endl; } //输出对应结果 if (!check(a)) puts("Not found in 10 iterations."); else print(a), cout << " is a palindromic number." << endl; return 0; }