Problem
https://uva.onlinejudge.org/external/100/10018.pdf
Solution
主要是reverse
函數, 完成了就差不多了
注意!! : 此題的數字範圍需要用到unsigned
Note: 我把輸入的地方跟解題邏輯拆開
#include<iostream>
using namespace std;
unsigned int reverse(unsigned int num)
{
unsigned int rev = 0;
while(num > 0)
{
rev = rev * 10 + num % 10;
num = num / 10;
}
return rev;
}
void solve(unsigned int num)
{
unsigned int count,rev;
count = 1;
rev = reverse(num);
while(1)
{
num = rev + num;
rev = reverse(num);
if(rev == num)
break;
count++;
}
cout << count << " " << rev << endl;
}
int main()
{
int testcase;
unsigned int num;
cin >> testcase;
while(testcase)
{
cin >> num;
solve(num);
testcase--;
}
return 0;
}