计蒜客习题:回文数

#include <iostream>
#include <vector>
#include <assert.h>

using namespace std;

vector<int> ans;
int rev(int x) {
    int res = 0;
    while (x) {
        res = res * 10 + x % 10;
        x /= 10;
    }
    return res;
}

int main() {
    int n;
    while (cin >> n) {
        ans.clear();
        ans.push_back(n);
        int temp = rev(n);
        while (temp != n) {
            n += temp;
            ans.push_back(n);
            temp = rev(n);
            assert(n > 0);
        }
        cout << ans.size() - 1 << endl;
        cout << ans[0];
        for (int i = 1; i < ans.size(); ++i) {
            cout << "--->" << ans[i];
        }
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yoga1976/article/details/88806272