hdu1282 回文数猜想

证明一个数是不是回文数。可以这样证明。

先对他倒置比如定义一个函数reverse(a)

如果a*2=reverse(a)+a 那么就是回文数了。

#include <iostream>
using namespace std;
int reverse(int val)
{
	int a,c=0;
	while(1)
	{
		a=val%10;
		if(val)
		{
			c=c*10+a;
		}
		else
			break;
		val/=10;
	}
	return c;
}
bool ishui(int a)
{
	int b = reverse(a);
	if(a+b==a*2)
		return true;
	else
		return false;
}
int main()
{
	int a;
	while(cin>>a)
	{
		int num[100],i=1;
		num[0]=a;
		while(1)
		{
			if(ishui(a))
			{
				num[i]=a;
				break;
			}
			a+=reverse(a);
			num[i]=a;
			i++;
		}
		cout<<i-1<<endl;
		for(int j=0;j<i;j++)
		{
			cout<<num[j];
			if(j==i-1)
				cout<<endl;
			else
				cout<<"--->";
		}

	}
}


 

posted on 2014-01-19 18:43  果冻虾仁  阅读(118)  评论(0编辑  收藏  举报

导航