输入一个数字,操作就是不停的把该数字翻转,然后二者相加。求问经过多少次能够得到一个回文数。
注意一下:回文数最大不超过4,294,967,295,所以使用int类型会溢出。
代码如下:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long ll;
int N,ans;
ll x;
string num2str(ll i){
stringstream s;
s<<i;
return s.str();
}
bool judge(ll num){
string s=num2str(num);
int left = 0, right = s.length()-1;
while (left<right){
if (s[left]!=s[right]) return 0;
left++;
right--;
}
return 1;
}
ll str2num(string s){
ll num;
stringstream ss(s);
ss >> num;
return num;
}
ll add(ll x){
string s = num2str(x);
reverse(s.begin(),s.end());
ll y = str2num(s);
return x+y;
}
int main(){
scanf("%d",&N);
while (N--){
scanf("%lld",&x);
ans = 0;
while (!judge(x)) {ans++; x=add(x);}
printf("%d %lld\n",ans,x);
}
return 0;
}