#include <bits/stdc++.h> using namespace std; #define ll long long string turn(string s) { reverse(s.begin(),s.end()); return s; } string add(string a,string b) { string c = a; int m = 0; for(int i = a.size()-1;i>=0;i--) { c[i] = (a[i]-‘0‘+b[i]-‘0‘+m)%10+‘0‘; m = (a[i]-‘0‘+b[i]-‘0‘+m)/10; } if(m>0) { c = ‘1‘+c; } return c; } int main() { string s,sum; int n=10; cin>>s; if(s == turn(s)){ cout<<s<<" is a palindromic number.\n"; return 0; } while(n--) { sum = add(s,turn(s)); cout<<s<<" + "<<turn(s)<<" = "<<sum<<endl; if(sum ==turn(sum)){ cout<<sum<<" is a palindromic number.\n"; return 0; } s =sum; } cout<<"Not found in 10 iterations.\n"; return 0; }
原文地址:https://www.cnblogs.com/tonyyy/p/10479557.html
时间: 2024-10-28 02:58:57