PAT_A1136#A Delayed Palindrome
Source:
Description:
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 with 0 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.
Input Specification:
Each input file contains one test case which gives a positive integer no more than 1000 digits.
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 = Cwhere
Ais the original number,Bis the reversedA, andCis their sum.Astarts being the input number, and this process ends untilCbecomes a palindromic number -- in this case we print in the last lineC is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, printNot found in 10 iterations.instead.
Sample Input 1:
97152
Sample Output 1:
97152 + 25179 = 122331 122331 + 133221 = 255552 255552 is a palindromic number.
Sample Input 2:
196
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.
Keys:
- 快乐模拟
Attention:
- under algorithm, reverse(s.begin(),s.end());
Code:
1 /* 2 Data: 2019-08-07 19:32:34 3 Problem: PAT_A1136#A Delayed Palindrome 4 AC: 17:12 5 6 题目大意: 7 非回文数转化为回文数; 8 while(! palindrome){ 9 1.Reverse 10 2.add 11 输入: 12 给一个不超过1000位的正整数 13 输出: 14 给出每次循环的加法操作,最多10次循环 15 */ 16 #include<cstdio> 17 #include<string> 18 #include<iostream> 19 #include<algorithm> 20 using namespace std; 21 22 bool IsPali(string s) 23 { 24 int len=s.size(); 25 for(int i=0; i<len/2; i++) 26 if(s[i] != s[len-1-i]) 27 return false; 28 return true; 29 } 30 31 string Func(string s1) 32 { 33 string s,s2=s1; 34 reverse(s2.begin(),s2.end()); 35 int carry=0; 36 for(int i=0; i<s1.size(); i++) 37 { 38 carry += (s1[i]-'0'+s2[i]-'0'); 39 s.insert(s.end(),'0'+carry%10); 40 carry /= 10; 41 } 42 while(carry!=0) 43 { 44 s.insert(s.end(),'0'+carry%10); 45 carry /= 10; 46 } 47 reverse(s.begin(),s.end()); 48 printf("%s + %s = %s\n", s1.c_str(),s2.c_str(),s.c_str()); 49 return s; 50 } 51 52 int main() 53 { 54 #ifdef ONLINE_JUDGE 55 #else 56 freopen("Test.txt", "r", stdin); 57 #endif // ONLINE_JUDGE 58 59 string s; 60 cin >> s; 61 for(int i=0; i<10; i++) 62 { 63 if(IsPali(s)) 64 { 65 printf("%s is a palindromic number.\n", s.c_str()); 66 s.clear();break; 67 } 68 s = Func(s); 69 } 70 if(s.size()) 71 printf("Not found in 10 iterations."); 72 73 return 0; 74 }
浙公网安备 33010602011771号