老是忘了特判!!
本身就是回文数忘了,俩个测试点没过
还有k==0的时候,好像这个测试点没测出来?不过我写的代码还是特判一下,不然出错了
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
void slove();
int main(){
slove();
return 0;
}
int k;
void func(string str, int num);
string Rev(string str);
string Plus(string a, string b);
bool isPalindromic(string);
void slove(){
string n;
cin >> n >> k;
if (isPalindromic(n)){
cout << n << endl << "0" << endl;
return;
}
if (k == 0){
cout << n << endl << k << endl;
return;
}
func(n, 1);
}
void func(string str, int num){
string rev = Rev(str);
string res = Plus(str, rev);
if (isPalindromic(res)){
cout << res << endl;
cout << num << endl;
return;
}
else if (num == k){
cout << res << endl << num << endl;
return;
}
else{
func(res, num + 1);
}
}
string Rev(string str){
string t="";
for (int i = 0; i < str.size(); ++i){
t = str.at(i) + t;
}
return t;
}
string Plus(string a, string b){
string res = "";
int lena = a.size();
int lenb = b.size();
int jw = 0;
int sum;
for (int i = lena-1, j=lenb-1; i >-1||j>-1; --i,--j){
if (i < 0) sum = b[j] - '0';
else if (j < 0) sum = a[i] - '0';
else sum = a[i] - '0' + b[i] - '0';
int c = sum + jw;
jw = c / 10;
c = c % 10;
char cc = c + '0';
res = cc + res;
}
while (jw){
int c = jw % 10;
char cc = c + '0';
res = cc + res;
jw /= 10;
}
return res;
}
bool isPalindromic(string str){
string s = str;
reverse(str.begin(), str.end());
if (s==str)
return true;
return false;
}