HDU 1282(回文数猜想)

基础题。

#include <iostream>
#include <cstring>
using namespace std;

int num[10000];

//计算n的回文数
int palindrome(int n)
{
	int temp = n;
	int pn = 0;
	while (temp > 0)
	{
		pn = pn * 10 + temp % 10;
		temp /= 10;
	}
	return pn;
}

int main()
{
	int N;
	while (cin >> N)
	{
		memset(num, 0, sizeof(num));
		int total = 0;
		if (N / 10 == 0) //个位数,一定是回文数
		{
			cout << total << endl << N << endl;
			continue;
		}

		num[total] = N;
		int PN; //当前数的回文数
		while (true)
		{
			PN = palindrome(num[total]);
			if (PN == num[total])
				break;
			num[total + 1] = num[total] + PN;
			++total;
		}

		cout << total << endl;
		for (int i = 0; i < total; i++)
			cout << num[i] << "--->";
		cout << num[total] << endl;
	}
	return 0;
}

继续加油。

发布了206 篇原创文章 · 获赞 1 · 访问量 8981

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/104855549