Day49: [PAT甲级] 1024 Palindromic Number (25分)
题源:
来自PAT真题库:
https://pintia.cn/problem-sets/994805342720868352/problems/994805476473028608
代码:
dirty code凑合看吧
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<unordered_map>
#include<queue>
#include<set>
#include<unordered_set>
#include<cstdio>
#include<algorithm>
using namespace std;
string N;
int K;
bool isF(string x) {
for (int i = 0, j = x.size() - 1; i <= j;) {
if (x[i++] != x[j--]) return false;
}
return true;
}
string F(string x) {
string y = x,res;
reverse(y.begin(), y.end());
int jinwei = 0;
for (int i = x.size() - 1;i>=0;i--) {
int X = (x[i] - '0') + (y[i] - '0') + jinwei;
if (X >= 10) {
jinwei = 1;
X -= 10;
}
else jinwei = 0;
res.append(1,'0'+X);
}
if (jinwei != 0) {
res.append(1, '0' + jinwei);
}
reverse(res.begin(), res.end());
return res;
}
int main() {
cin >> N >> K;
int i = 0;
if (isF(N)) {
cout << N << endl;
cout << 0;
}
else {
for (;i<K;i++) {
N = F(N);
if (isF(N)) {
cout << N << endl;
cout << i + 1;
break;
}
}
}
if (i >= K) {
cout << N << endl;
cout << K;
}
system("pause");
}