poj 2402 Palindrome Numbers

#include <iostream>
#include
<string>
#include
<math.h>
using namespace std;
__int64 list[
20]={0,9,9,90,90,900,900,9000,9000,90000,90000,900000,900000,9000000,9000000,
90000000,90000000,900000000,900000000,9000000000};
//list[i]表示位数为i的满足回文数要求共有list[i]个
int main()
{
__int64 n,sum;
int i;
while(cin>>n&&n)
{
sum
=0;
for(i=1;i<=19;++i)
{
if(sum+list[i]>=n) //i表示位数
break;
sum
+=list[i];
}
string str(i,'0');
str[
0]=str[i-1]='1';
n
=n-sum-1;
if(i==1)
{
str[
0]+=n;
}
else
{
int index=-1,s=(int)pow(10.0,(i-2)/2+(i-2)%2);
for( ;s>=1 && n!=0 ;s/=10 )
{
index
++;
if(n<s)
continue;
str[index]
+=n/s; //不能写成=n/s,因为最外层的str[0],str[i-1]起始值是1
str[i-1-index]=str[index];
n
=n%s;
}
}
cout
<<str<<endl;
}
return 0;
}

  

posted on 2011-07-22 15:03  sysu_mjc  阅读(136)  评论(0编辑  收藏  举报

导航