-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpat_advance_1024.cpp
60 lines (59 loc) · 1.14 KB
/
pat_advance_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
57
58
59
60
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> vTemp;
vector<int> vCom;
bool isPalindromic(vector<int> &v){
bool result;
int i = 0, j = v.size() - 1;
while (i <= j){
if (v[i] != v[j])break;
i++, j--;
}
i <= j ? result = false : result = true;
return result;
}
int main(){
string str;
int k, step = 0;
cin >> str >> k;
for (int i = str.size() - 1; i >= 0; i--)
{
vTemp.push_back(str[i] - '0');
}
if (isPalindromic(vTemp) == true){
cout << str << endl;
cout << '0';
}
else{
vCom = vTemp;//when the k equal to 0 do not foget
while (step < k){
vCom.clear();
step++;
int j = vTemp.size() - 1;
int ge = 0, shi = 0;
//the add operation is not same as multi
for (int i = 0; i < vTemp.size(); i++, j--)
{
ge = shi + (vTemp[i] + vTemp[j]) % 10;
shi = ge / 10 + (vTemp[i] + vTemp[j]) / 10;
vCom.push_back(ge % 10);
}
if (shi != 0){
vCom.push_back(shi);
}
if (isPalindromic(vCom) == true){
break;
}
vTemp = vCom;
}
for (int i = vCom.size() - 1; i >= 0; i--)
{
cout << vCom[i];
}
cout << endl << step;
}
return 0;
}