分值 : 20 难度 : 简单题 思路 : string 模拟大树运算 坑点 : 暂未发现
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
#include <iostream>#include <algorithm>using namespace std;bool judge(string s){ string temp = s; reverse(temp.begin() , temp.end()) ; return s == temp ;}string deal(string s){ string temp = s ; reverse(temp.begin(),temp.end()) ; int Cy = 0 ; string result ; for(int i = s.size()-1 ; i>= 0 ; i--) { int c1 = s[i] - '0' ; int c2 = temp[i] - '0' ; int end = c1 + c2 +Cy ; if(end >= 10) { Cy = 1 ; end -=10 ; } else Cy = 0 ; char ch = '0'+ end ; result += ch ; } if(Cy) result += '1' ; reverse(result.begin() , result.end()) ; return result ;}int main() { string s ; cin >> s; int count = 0 ; while(count <10) { if(judge(s)) { cout << s <<" is a palindromic number."<<endl ; break ; } else { string s2 = s ; reverse(s2.begin() ,s2.end()) ; cout << s <<" + "<<s2 <<" = "<< deal(s) <<endl ; s = deal(s) ; } count ++ ; } if(count >= 10 ) cout <<"Not found in 10 iterations."<<endl;}