hoj1384 Palindrome Number

#include <iostream>
#include <memory.h>
using namespace std;

int pow(int x)
{
	int a=1;
	for(int i=1;i<=x;i++)
		a=10*a;
		return a;
}

int ln(int x)
{
	int i=1;
	while(1)
	{
		if(pow(i)<=x)
			i++;
		else break;
	}
	return --i;
}

void half(int x,int h)
{
	if(h==2)
	{
		while(x>0)
		{
			cout<<(x%10);
			x=x/10;
		}
	}
	else if(h==1)
	{
		x=x/10;
		while(x>0)
		{
			cout<<(x%10);
			x=x/10;
		}
	}
	return;
}
int main(int args,char** argv)
{
	int i,n,left;
	while(cin>>n)
	{
		i=ln(n/2+1);
		left=n-2*(pow(i)-1);
		if(left==0) 
		{
			int ans=pow(i)-1;
			cout<<ans;
			half(ans,2);
			cout<<endl;
		}
		else if(left<=9*pow(i))
		{
			int ans=pow(i)+left-1;
			cout<<ans;
			half(ans,1);
			cout<<endl;
		}
		//if(left>9*pow(i))
		else
		{  
			int ans;
			left=left-9*pow(i);
			ans=pow(i)+left-1;
			cout<<ans;
			half(ans,2);
			cout<<endl;
		}
		
	}
	return 0;
}

 

posted @ 2012-02-06 13:18  wuzhibin  阅读(133)  评论(0)    收藏  举报