-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1079.cpp
More file actions
61 lines (54 loc) · 1.61 KB
/
1079.cpp
File metadata and controls
61 lines (54 loc) · 1.61 KB
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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct BigInt {
vector<int> digits;
BigInt(vector<int> digits) : digits(digits) {}
BigInt reversed() {
vector<int> tmp(digits);
reverse(tmp.begin(), tmp.end());
return BigInt(tmp);
}
bool is_palindromic() {
for (int i = 0, j = digits.size() - 1; i < j; i++, j--)
if (digits[i] != digits[j])
return false;
return true;
}
friend BigInt operator+(BigInt a, BigInt b) {
const int n = a.digits.size(), m = b.digits.size();
vector<int> tmp(max(n, m));
int carry = 0;
for (int i = 0; i < tmp.size(); i++) {
int sum = carry;
sum += i < n ? a.digits[i] : 0;
sum += i < m ? b.digits[i] : 0;
tmp[i] = sum % 10;
carry = sum / 10;
}
if (carry) tmp.push_back(carry);
return BigInt(tmp);
}
friend ostream& operator<<(ostream &out, const BigInt &x) {
for (auto it = x.digits.rbegin(); it != x.digits.rend(); it++)
out << *it;
return out;
}
};
int main() {
char c;
int count = 0;
vector<int> tmp;
while (cin >> c) tmp.push_back(c - '0');
reverse(tmp.begin(), tmp.end());
BigInt a(tmp);
while (count < 10 && !a.is_palindromic()) {
BigInt b = a.reversed();
cout << a << " + " << b << " = " << (a = a + b) << endl;
count++;
}
if (count < 10) cout << a << " is a palindromic number." << endl;
else cout << "Not found in 10 iterations." << endl;
return 0;
}