PAT Baisc 1079 palindrome delay (20 minutes)

Given a  K + . 1-bit positive integer  N, written as  A K A . 1 A 0 form, wherein of all  there is I  0 and  A K > 0. N is called a palindrome, if and only if for all  have I  A I = A K - I . Zero is also defined as a palindrome.

The number of non-palindromic series of operations can also change the palindrome. The first digital reversed, and then added to the number of reversal number, and if not a palindrome Repeat this operation to reverse again added until a palindrome appears. If a non-palindromic number can change a palindrome, and called the number palindrome delay. (Translated from the definition  https://en.wikipedia.org/wiki/Palindromic_number  )

Given any positive integer, this question asks you to find out the number of variants of the palindrome.

Input formats:

Input gives a positive integer of not more than 1,000 in a row.

Output formats:

For a given integer, line by line and outputs the number of variants of a process palindrome. Each line has the form

A + B = C

Which  A is the original number, B is  A the number of reversal C is their sum. A Starting integer input. Repeat until  C becomes palindrome within 10 steps, in this case the output line  C is a palindromic number.; the step 10, or if failed to get palindrome, the final outputs in a row  Not found in 10 iterations..

Sample Input 1:

97152

Output Sample 1:

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Sample Input 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.


#include <iostream>
#include <algorithm>
using namespace std;
string plus_str(string s1,string s2){
    reverse(s1.begin(),s1.end());
    reverse(s2.begin(),s2.end());
    string res="";int i;bool jinwei=false;
    for(i=0;i<s2.size();i++){
        if(jinwei) {
            res+=((s1[i]-'0'+s2[i]+1-'0')%10+'0');
            jinwei=false;
            if((s1[i]-'0'+s2[i]-'0'+1)/10>0) jinwei=true;
        }
        else {
            res+=((s1[i]-'0'+s2[i]-'0')%10+'0');
            if((s1[i]-'0'+s2[i]-'0')/10>0) jinwei=true;
        }
    }
    if(jinwei) res+='1';
    reverse(res.begin(),res.end());
    return res;
}
int main()
{
    string str,rev,add;int n=10;
    cin>>str;
    while(n--){
        rev=str;
        reverse(rev.begin(),rev.end());
        if(str==rev) {
            printf("%s is a palindromic number.",str.data());
            system("pause");
            return 0;
        }
        add=plus_str(str,rev);
        printf("%s + %s = %s\n",str.data(),rev.data(),add.data());
        str=add;
    }
    printf("Not found in 10 iterations.");
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11707164.html