Poj Solution 2402

http://poj.org/problem?id=2402

#include<iostream>
#include<cmath>
using namespace std;
//int a[20]={9,9,90,90,900,900,9000,9000,90000,90000,900000,900000,9000000,9000000,90000000};//x位上的回文数个数为f(x)=9*10^((x+1)/2);
__int64 b[19]={0,9,18,108,198,1098,1998,10998,19998,109998,199998,1099998,1999998,
                10999998,19999998,109999998,199999998,1099999998,1999999998};//前x位上的回文数总个数
int c[10];
int main()
{
__int64 i,j,n,d,k,l;
double temp;
while(scanf("%I64d",&i)!=EOF&&i)
{
        for(j=0;;j++)
   {
    if(i<=b[j])
     break;
   }
     //j回文数的位数
   n=b[j-1];//1...n-1 数的回文数总数
   d=i-n-1;
     temp=(j+1)/2-1;
   k=(int)pow(10.0,temp);
   k+=d;
   l=k;
   if(j%2==1)
     l=k/10;
   for(j=0;l>0;j++)
   {
    c[j]=l%10;//前半部分和后半部分对称
    l/=10;
   }
   printf("%I64d",k);//输出前半部分
   for(i=0;i<j;i++)//输出后半部分
    printf("%d",c[i]);
   printf("n");
}
return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *