题干
测试点2、3、4:如果输入既是回文数,则判断后直接输出即可。
#include<bits/stdc++.h>
using namespace std;
bool ishuiwen(string str) {
int a=0,b=str.size()-1,flag=1;
while(a<=b) {
if(str[a]!=str[b]) {
flag=0;
break;
}
a++,b--;
}
if(flag) return true;
return false;
}
int main() {
string s;
cin>>s;
if(ishuiwen(s)) {
cout<<s<<" is a palindromic number.";
return 0;
}
for(int i=0; i<10; i++) {
string s1=s,s2=s;
reverse(s2.begin(),s2.end());
string ans="";
int x,jinwei=0,wei;
for(int j=0; j<s.size(); j++) {
x=s1[j]-'0'+s2[j]-'0'+jinwei;
wei=x%10;
jinwei=x/10;
ans+=wei+'0';
}
if(jinwei) ans+=jinwei+'0';
reverse(ans.begin(),ans.end());
cout<<s1<<" + "<<s2<<" = "<<ans<<endl;
if(ishuiwen(ans)) {
cout<<ans<<" is a palindromic number.";
return 0;
} else {
s=ans;
}
}
cout<<"Not found in 10 iterations.";
return 0;
}