-
Notifications
You must be signed in to change notification settings - Fork 1
/
P10018.cpp
65 lines (59 loc) · 1.2 KB
/
P10018.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
struct Integer {
unsigned int digits[11];
int len;
Integer(unsigned long long P) : len(0) {
while(P > 0) {
digits[len++] = P%10;
P/=10;
}
}
bool isPalindrome() {
for(int i = 0; i < len/2; ++i) {
if(digits[i] != digits[len-1-i])
return false;
}
return true;
}
void print() {
for(int i = 0; i < len; ++i) {
std::cout << digits[i];
}
}
void reverseAndAdd() {
//print(); std::cout << " -> ";
unsigned int tmp[11];
for(int i = 0; i < len; ++i) {
tmp[i] = digits[i];
digits[i] = 0;
}
digits[len] = 0;
for(int i = 0; i < len; ++i) {
digits[i]+= tmp[i] + tmp[len-1-i];
if(digits[i] > 9) {
digits[i+1] += digits[i]/10;
digits[i] = digits[i]%10;
}
}
if(digits[len] > 0)
++len;
//print(); std::cout << std::endl;
}
};
int main() {
int N;
std::cin >> N;
for(int ignore = 0; ignore < N; ++ignore) {
unsigned long long P;
std::cin >> P;
int iterations = 0;
Integer i(P);
while(!i.isPalindrome()) {
++iterations;
i.reverseAndAdd();
}
std::cout << iterations << " ";
i.print();
std::cout << std::endl;
}
}