-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1024.cpp
56 lines (51 loc) · 993 Bytes
/
1024.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
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long lint;
bool isPalind(string s) {
for(int i = 0, j = s.size()-1; i < j; ++i, --j){
if(s[i] != s[j])
return false;
}
return true;
}
string bigPlus(string a, string b) {
string result = "";
vector<int> v;
for(unsigned int i = 0; i < a.size(); ++i) {
v.push_back(a[i] - '0' + b[i] - '0');
}
int borrow = 0;
for(int i = v.size()-1; i >= 0; --i) {
v[i] += borrow;
borrow = v[i] / 10;
result.push_back('0' + v[i] % 10);
}
if(0 != borrow)
result.push_back('0' + borrow);
reverse(result.begin(), result.end());
return result;
}
int main(int argc, char const *argv[])
{
string a;
int k;
cin >> a >> k;
int i = 0;
for(; i < k; ++i) {
if(isPalind(a)) {
break;
}
string b = a;
reverse(b.begin(), b.end());
a = bigPlus(a, b);
}
cout << a << endl;
cout << i << endl;
return 0;
}