PAT Grade 1024 Palindromic Number (25 minutes) (addition of large numbers, consider this number is not a palindrome sequence beginning)

1024 Palindromic Number (25 分)
 

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

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. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤) is the initial numer and K (≤) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and Kinstead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

answer:

  CONSIDERING the outset with the addition of large numbers just in case. First submission found 2 test points and test points 3 have not seen their own analysis of the reasons may have been before the string is a palindrome without, that is, k = 0, there is a single digit palindrome string.

5 100
5
0
131 500
131

  This title examines a number of addition and reverse, this belongs to the simple question, but the nature of the test Tarsus sum of knowledge. Not adding lead to large numbers noted test points 6 and 8 test points (starting from 0) is not adopted. The reason is this question is N range (0,1010], the range of k is (0, 100], we consider the worst case, assuming that N is a very approximate value of 1010 and 100 were still poorly step operation value palindrome , is simply the maximum calculated estimation understood encountered is 2100 * 1010, the value exceeds the long long int (263-1, approximately 9.2 * 1018) indicates a range, it is necessary char value stored number.

AC Code:

#include <bits / STDC ++ H.>
 the using  namespace STD;
 char A [ 101 ];
 char B [ 101 ];
 char C [ 101 ];
 int n-;
 int main () { 
    CIN >> A; 
    CIN >> n-;
     int = L strlen (A);
     for ( int I = 0 ; I <L; I ++ ) { 
        B [I] = A [Li- . 1 ]; 
    } 
    // check generation 0 is not itself palindromic 
    int F = 1 ; 
     int mid=(l-1)/2;
    for(int j=0;j<=mid;j++){
        if(a[j]!=a[l-j-1]){
            f=0;
            break;
        }
    }
    if(f){
        cout<<a<<endl;
        cout<<0;
    }
    else{//再考虑第1代及以后 
        int k=-1;
        for(int i=1;i<=n;i++) {
             // add up a new value 
            int X = 0 ;
             for ( int J = 0 ; J <L; J ++ ) { 
                X = A [J] - ' 0 ' + B [J] - ' 0 ' + X; 
                C [J] = X% 10 + ' 0 ' ; 
                X = X / 10 ; 
            } 
            IF (X> 0 ) { 
                C [L ++] = X + ' 0 '  ;
            }
             // check the character does not meet the requirements 
            MID = (L- . 1 ) / 2 ; 
            F = . 1 ;
             for ( int J = 0 ; J <= MID; J ++ ) {
                 IF ! (C [J] = C [lj- . 1 ]) { 
                    F = 0 ;
                     BREAK ; 
                } 
            } 
            IF (F) { // palindromic 
                K = I;
                 BREAK ; 
            } 
            for ( int= J endl;0 ; J <L; J ++) { // update 
                A [J] = C [J]; 
                B [J] = C [lj- . 1 ]; 
            }          
        } 
        for ( int I = L- . 1 ; I> = 0 ; I -) { // output 
            COUT << C [I]; 
        } 
        COUT << endl;
         IF ! (K = - . 1 ) { // not to n generations 
            COUT << K << endl; 
        } the else {      
            COUT << << n- 
        }    
    }
    return 0;
} 

 

 

Learn about other people too concise Code:

#include <iostream>
#include <algorithm>
using namespace std;
string add(string a){
    string ans=a;
    reverse(a.begin(),a.end());
    int i=a.length()-1,add=0;
    while(i>=0){
        int tmp=a[i]-'0'+ans[i]-'0';
        ans[i]=(add+tmp)%10+'0';
        add=(tmp+add)/10;
        i--;
    }
    if(add) ans.insert(0,"1");
    return ans;
}
int main(){
    string s;
    int k;
    cin>>s>>k;
    string tmp=s;
    reverse(tmp.begin(),tmp.end());
    if(tmp==s) cout<<tmp<<endl<<0;
    else{
        int i=0;
        while(i<k){
            s=add(tmp);
            i++;
            tmp=s;
            reverse(tmp.begin(),tmp.end());
            if(tmp==s) break;
        }
        cout<<s<<endl<<i;
    } 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/caiyishuai/p/11360919.html
Recommended