UVa 10018 Reverse and Add (数学&利克瑞尔数)

262 篇文章 0 订阅
100 篇文章 0 订阅

10018 - Reverse and Add

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=959

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

In this particular case the palindrome 9339 appeared after the 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a 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.

The Output

For each of the N tests you will have to write a line with the following data : minimum number of iterations (additions) to get to the palindrome and the resulting palindrome itself separated by one space.

Sample Input

3
195
265
750

Sample Output

4 9339 
5 45254 
3 6666


利克瑞尔数(Lychrel number)相关资料:

1. 维基百科

2. 10110是利克瑞尔数的证明

3. 更详细的研究

摘录:“Expected Maximum Iteration Depth = 14.416667 * Digit Length - 18.338235”


4. 整数序列大百科A023108

5. 优化:可以通过map来将已计算过的存储起来(坑)


完整代码:

/*0.022s*/

#include<cstdio>

int main(void)
{
	int t;
	long long ans, s, p, count, temp;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%lld", &p);
		count = 0;
		while (true)
		{
			ans = 0;
			for (temp = p; temp; temp /= 10)
				ans = 10 * ans + temp % 10;
			if (ans == p) break;///ans是回文数
			else
			{
				count++;
				p += ans;
			}
		}
		printf("%lld %lld\n", count, ans);
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值