简洁的解法
#include <iostream>
#include<algorithm>
using namespace std;
string add(string a, string b)
{
string ans = "\0";
int flag = 0;//进位
for (int i = a.length() - 1; i >= 0; i--)
{
int res = a[i] - '0' + b[i] - '0'+flag;
if (res >= 10)
{
res -= 10;
flag = 1;
}
else
flag = 0;
ans += res + '0';
}
if (flag)
ans += '1';
reverse(ans.begin(), ans.end());
return ans;
}
int main(void)
{
int n = 0;
string b;
cin >> a;
while (n++<10)
{
string b = a;
reverse(b.begin(),b.end());
if (a == b)//判断是否是Palindrome
break;
string c = add(a, b);
cout << a << " + " << b << " = " << c << endl;
a = c;
}
if (n >= 10)
cout << "Not found in 10 iterations." << endl;
else
cout << a << " is a palindromic number." << endl;
return 0;
}