1079 延迟的回文数 (20 分)

1079 延迟的回文数 (20 分)_第1张图片

#include 
#include
#include
#include
using namespace std;
bool check(int a[],int n) {//判断是否为回文数
    bool flag = true;
    if (n == 1 && a[0] == 0) {
        return true;
    }
    if (a[0] <= 0) {
        flag = false;
    }
    for (int i = 0;i <= n / 2;i++) {
        if (a[i] != a[n - i-1]) {
            flag = false;
        }
    }
    return flag;
}
int main()
{
    string str;
    int count = 0;
    bool flag = true;
    cin >> str;
    int len = str.length();
    vector c(len);
    for (int i = 0;i < len;i++) {
        c[i] = str[i] - '0';
    }
    if (check(&c[0], c.size())) {
        for (int i = 0;i < c.size();i++)
            cout << c[i];
        cout << " is a palindromic number.";
        return 0;
    }
    while (count < 10) {
        int x = 0;
        len = c.size();
        vector a(len);
        vector b(len);
        for (int i = 0;i < len;i++) {
            a[i] = c[i];
            b[len - i - 1] = c[i];
        }
        for (int i = 0;i < len;i++) {
            x = x + a[i] + b[i];
            c[i] = x % 10;
            x = x / 10;
        }
        if (x != 0) {
            c.push_back(x);
        }
        reverse(c.begin(), c.end());
        if (check(&c[0], c.size())) {
            for (int i = 0;i < a.size();i++)
                cout << a[i];
            cout << " + ";
            for (int i = 0;i < b.size();i++)
                cout << b[i];
            cout << " = ";
            for (int i = 0;i < c.size();i++)
                cout << c[i];
            cout << endl;
            for (int i = 0;i < c.size();i++)
                cout << c[i];
            cout << " is a palindromic number.";
            return 0;
        }
        else {
            for (int i = 0;i < a.size();i++)
                cout << a[i];
            cout << " + ";
            for (int i = 0;i < b.size();i++)
                cout << b[i];
            cout << " = ";
            for (int i = 0;i < c.size();i++)
                cout << c[i];
            cout << endl;
        }
        count++;
    }
    if (count >= 10) {
        cout << "Not found in 10 iterations.";
    }

    return 0;
}

你可能感兴趣的:(1079 延迟的回文数 (20 分))