yipo
9/23/2017 - 3:38 PM

uva10018

Reverse and Add

#include <iostream>
using namespace std;

uint32_t reverse(uint32_t num)
{
    uint32_t result = 0;
    while (num)
    {
        result *= 10;
        result += num % 10;
        num /= 10;
    }
    return result;
}

bool is_palindrome(uint32_t num)
{
    return num == reverse(num);
}

void test_case()
{
    uint32_t num;
    cin >> num;

    size_t iter = 0;
    while (!is_palindrome(num))
    {
        num += reverse(num);
        iter++;
    }

    cout << iter << " " << num << endl;
}

int main()
{
    size_t n;
    cin >> n;
    while (n--) test_case();
    return 0;
}