其他分享
首页 > 其他分享> > 1136 A Delayed Palindrome (20 分)

1136 A Delayed Palindrome (20 分)

作者:互联网

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool check( string A )
{
    for( int i = 0; i < A.size() / 2; ++i )
        if( A[i] != A[ A.size() - 1 - i ] )
            return false;
    return true;
}
int main()
{
    string A, B;
    cin >> A;
    int cnt;
    for( cnt = 0; cnt < 10; ++cnt )
    {
        if( check( A ) )
        {
            cout << A << " is a palindromic number.";
            return 0;
        }
        cout << A << " + ";
        B = A;
        reverse( A.begin(), A.end() );
        cout << A << " = ";
        for( int i = 0; i < A.size(); ++i )
        {
            A[i] += B[i] - '0';
            if( i == A.size() - 1 ) break;
            A[i + 1] += ( A[i] - '0' ) / 10;
            A[i] = ( A[i] - '0' ) % 10 + '0';
        }
        if( A.back() > '9' )
        {
            A.push_back( ( A.back() - '0' ) / 10 + '0' );
            A[ A.size() - 2 ] = ( A[ A.size() - 2 ] - '0' ) % 10 + '0';
        }
        reverse( A.begin(), A.end() );
        cout << A << endl;
    }
    cout << "Not found in 10 iterations.";
}

标签:10,cnt,Palindrome,20,cout,int,1136,include,size
来源: https://blog.csdn.net/qq_43749739/article/details/100403577