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.";
}

上一篇:js验证日期是否正确


下一篇:OC Control SelectDate(日期选择器)