forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10018.cpp
47 lines (42 loc) · 776 Bytes
/
10018.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
unsigned long reverse(unsigned long n)
{
unsigned long rev_n = 0;
while (n != 0)
{
rev_n = (rev_n * 10) + n % 10;
n /= 10;
}
return rev_n;
}
inline int is_palindrome(unsigned long n)
{
return n == reverse(n);
}
int reverse_and_add(unsigned long num, unsigned long &result)
{
int counter = 0;
do
{
num += reverse(num);
counter++;
} while (!is_palindrome(num));
result = num;
return counter;
}
int main()
{
int numOfCase;
scanf("%d", &numOfCase);
while (numOfCase--)
{
unsigned long num;
scanf("%lu", &num);
int iteration; // #iteration
unsigned long result; // final palindrome, like 9339
iteration = reverse_and_add(num, result);
printf("%d %lu\n", iteration, result);
}
return 0;
}