The Problem

The “reverse and add” method is simple: choose a number, reverse its digits and add it to the original.

If the sum is not a palindrome (which means, it is not the same number from left to right and right to left),

repeat this procedure.

For example:

195 Initial number

591

—–

786

687

—–

1473

3741

—–

5214

4125

—–

9339 Resulting palindrome

Task

You must write a program that give the resulting palindrome and the number of iterations (additions) to compute the palindrome.

You might assume that all tests data on this problem:

– will have an answer ,

– will be computable with less than 1000 iterations (additions),

– will yield a palindrome that is not greater than 4,294,967,295.

The Input

The first line will have a number N with the number of test cases, the next N lines will have a number P to compute its palindrome.

3

195

265

750

The Output

4 9339

5 45254

3 6666

//unsigned int %u 4byte 0 to 4,294,967,295

//int %d 4byte –2,147,483,648 to 2,147,483,647

//long long %lld 8byte –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

#include<stdio.h> int main() { long long n,a,s,c; int t,i; while(scanf("%d",&t)==1) /* while scanf performed 1 assignment 用!= EOF好像也行 */ { for(i=1;i<=t;i++) { scanf("%lld",&n); //這題用%u 其實就可以了 a=n; //a暫存一開始的n值 c=0; while(1) { s=0; while(n!=0) { s=s*10+n%10; //取reverse最簡單的方法! n/=10; } if(s==a) break; else { n=s+a; a=s+a; c++; } } printf("%lld %lldn",c,a); } } return 0; }