1079 Delayed palindromes (20 minutes)
Given a positive integer N with k+1 bits, it is written in the form of a k a 1 a 0, where for all I there is 0 < a I < 10 and a k > 0.N is called a palindrome number if and only if there is a i = a k_i for all I.Zero is also defined as a palindrome number.
The number of non-palindromes can also be changed through a series of operations.First the number is reversed, then the number of reversals is added to the number. If the sum is not a palindrome number, repeat the operation until a palindrome number appears.If a non-palindrome number can change to a palindrome number, it is called the delayed palindrome number.(Definition translated from) https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, this question asks you to find the palindrome from which it changes.
Input format:
The input gives a positive integer not exceeding 1000 bits in a line.
Output format:
For a given integer, the process of outputting its palindrome number one line at a time.Each line is formatted as follows
A + B = C
Where A is the original number, B is the inverse number of A, and C is their sum.A Begins with the integer entered.Repeat until C becomes palindromic number in less than 10 steps, then output C is a palindromic number. in one line, or if 10 steps do not get palindromic number, output Not found in 10 iterations. in one line.
Input Sample 1:
97152
Output Sample 1:
97152 + 25179 = 122331 122331 + 133221 = 255552 255552 is a palindromic number.
Input Sample 2:
196
Output Sample 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.
Simple questions, just change
Just sort out the two functions that determine whether palindromes are palindromes or not, and add them together
Since the title is limited to 1,000 bits, add large numbers and use string's addition or subtraction
Be careful:
1.s[i]!=s[s.length()-1-i], to be subtracted by 1 because length() returns a length that i s not the last subscript
2. I temporarily forgot how string s and int s convert to each other, but there happens to be one, just add'0'and force char
s3=(char)(a%10+'0')+s3;
No more questions
native code
#include<iostream> using namespace std; string s; bool ishui(string s){ for(int i=0;i<s.length()/2;i++){ if(s[i]!=s[s.length()-1-i]){ return false; } } return true; } string add(string s){ string s2=s; for(int i=0;i<s.length();i++){ s2[i]=s[s.length()-1-i]; } string s3; int a=0; for(int i=s.length()-1;i>=0;i--){ a=(s2[i]-'0')+(s[i]-'0')+a; s3=(char)(a%10+'0')+s3; a=a/10; } if(a>0){ s3=(char)(a%10+'0')+s3; } cout<<s<<" + "<<s2<<" = "<<s3<<endl; return s3; } int main(){ cin>>s; for(int i=0;i<10;i++){ if(ishui(s)){ cout<<s<<" is a palindromic number."; return 0; } else { s = add(s); } } cout<<"Not found in 10 iterations."; return 0; }
Or to put out the code of Willow to compare
#include <iostream> #include <algorithm> using namespace std; string rev(string s) { reverse(s.begin(), s.end()); return s; } string add(string s1, string s2) { string s = s1; int carry = 0; for (int i = s1.size() - 1; i >= 0; i--) { s[i] = (s1[i] - '0' + s2[i] - '0' + carry) % 10 + '0'; carry = (s1[i] - '0' + s2[i] - '0' + carry) / 10; } if (carry > 0) s = "1" + s; return s; } int main() { string s, sum; int n = 10; cin >> s; if (s == rev(s)) { cout << s << " is a palindromic number.\n"; return 0; } while (n--) { sum = add(s, rev(s)); cout << s << " + " << rev(s) << " = " << sum << endl; if (sum == rev(sum)) { cout << sum << " is a palindromic number.\n"; return 0; } s = sum; } cout << "Not found in 10 iterations.\n"; return 0; }