Reverse and Add

2020年1月17日 894点热度 0人点赞 0条评论

Reverse and Add

时间: 1ms        内存:64M

描述:

The reverse and add function starts with a number, reverses its digits and adds the reverse to the original. If the sum is not a palindrome (meaning it does not give the same number read from left to right and right to left), we repeat this procedure until it does.

For example, if we start with 195 as the initial number, we get 9,339 as the resulting palindrome after the fourth addition:

195 786 1,473 5,214
591 687 3,741 4,125
+ --- + --- + --- + ---
786 1,473 5,214 9,339

This method leads to palindromes in a few steps for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It has never been proven, however, that no such palindrome exists.

You must write a program that takes a given number and gives the resulting palindrome (if one exists) and the number of iterations/additions it took to find it.

You may assume that all the numbers used as test data will terminate in an answer with less than 1,000 iterations (additions), and yield a palindrome that is not greater than 4,294,967,295.

输入:

The first line will contain an integer N ( 0 < N100), giving the number of test cases, while the next N lines each contain a single integer P whose palindrome you are to compute.

输出:

For each of the N integers, print a line giving the minimum number of iterations to find the palindrome, a single space, and then the resulting palindrome itself.

示例输入:

3
195
265
750

示例输出:

4 9339
5 45254
3 6666

提示:

参考答案:

解锁文章

没有看到答案?微信扫描二维码可免费解锁文章

微信扫描二维码解锁

使用微信扫描二维码打开广告页面后可以立即关闭,再刷新此页面即可正常浏览此文章

所跳转广告均由第三方提供,并不代表本站观点!

已经扫描此二维码?点此立即跳转

code

这个人很懒,什么都没留下

文章评论