您的位置:首页 > 其它

UVa 10018 - Reverse and Add

2012-12-13 15:33 323 查看
题目:给定把一个数字a,每次变为a和a的反转的加和,的那个a自反是停止。

分析:简单题。直接计算即可。

注意:数据范围超过int。

#include <iostream>
#include <cstdlib>

using namespace std;

typedef long long LL;

LL rev( LL n )
{
	LL v = 0LL;
	while ( n ) {
		v *= 10LL;
		v += n%10LL;
		n /= 10LL;
	}
	return v;
} 

int main()
{
	int T; 
	while ( cin >> T ) 
	for ( int t = 1 ; t <= T ; ++ t ) {
		LL a,b;
		cin >> a;
		b = rev(a);
		int count = 0;
		while ( a != b ) {
			a = a+b;
			b = rev(a);
			count ++;
		}
		cout << count << " " << a << endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: