PC/UVa:110502/10018
可以使用to_string()
转换成string
,然后使用反向迭代器逆转,最后再使用stoull()
转换回unsigned long long
。
#include <iostream>
#include <string>
using namespace std;
unsigned long long getReverse(unsigned long long ll)
{
unsigned long long llRet = 0;
while (ll != 0){
llRet = llRet * 10 + ll % 10;
ll /= 10;
}
return llRet;
}
bool isPalindrome(unsigned long long ll)
{
string strNum = to_string(ll);
for (size_t i = 0; i + i < strNum.size(); i++)
{
if (strNum[i] != strNum[strNum.size() - 1 - i]) return false;
}
return true;
}
int main()
{
int N = 0;
cin >> N;
for (int n = 0; n < N; n++)
{
unsigned long long ll, llRev;
cin >> ll;
for (int cnt = 0; cnt < 1000; cnt++)
{
llRev = getReverse(ll);
//cout << llRev << endl;
ll += llRev;
if (isPalindrome(ll)){
cout << cnt + 1 << ' ' << ll << endl;
break;
}
}
}
return 0;
}
/*
3
195
265
750
*/