代码改变世界

POJ1019

2012-01-17 16:41 by Jeff, ... 阅读, ... 评论, 收藏, 编辑
#include "stdio.h"
#include <math.h>
#define SUM_MAX1 45
#define SUM_MAX2 9045
#define SUM_MAX3 1395495
#define SUM_MAX4 189414495
char pre45bits [46]={"112123123412345123456123456712345678123456789"};
unsigned int i,num,position,pos,maxnumber,minnumber,tempvalue1,tempvalue2,current,gap,minnumber2;
unsigned int calc1,calc2,calc3,x,y,z;
unsigned int total=0;
double p=0;
double bits;

void getValue2(unsigned int pos){
     minnumber=9;
     while(minnumber<100){
          tempvalue1=minnumber*minnumber - 8*minnumber+36;
          tempvalue2=(minnumber+1)*(minnumber+1)-8*(minnumber+1)+36;
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
    if(gap==0){
          printf("%dn",minnumber%10);
       }
    else if(gap <10){
          printf("%dn",gap);
       }
    else{
           calc1 = (gap-9)/2;
           if(((gap-9)%2)==0){
             printf("%dn",(calc1+9)%10);
           }else{
             printf("%dn",(calc1+10)/10);
          }
       }
}

void getValue3(unsigned int pos){
     minnumber=99;
     while(minnumber<1000){
          tempvalue1=minnumber*189-9666+3*(minnumber-98)*(minnumber-99)/2;
          minnumber2=minnumber+1;
          tempvalue2=minnumber2*189-9666+3*(minnumber2-98)*(minnumber2-99)/2;
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
        if(gap==0){
          printf("%dn",minnumber%10);
       }
       else if(gap <10){
             printf("%dn",gap);
       }else if(gap-9 <=180){
          calc1 = (gap-9)/2;
          if(((gap-9)%2)==0){
             printf("%dn",(calc1+9)%10);
           }else{
             printf("%dn",(calc1+10)/10);
          }
       }else{
          calc2 = (gap-189)/3;
          if(((gap-189)%3)==1){
             printf("%dn",(calc2+100)/100);
          }
          if(((gap-189)%3)==2){
             printf("%dn",((calc2+100)%100)/10);
          }
          if(((gap-189)%3)==0){
             printf("%dn",(calc2+99)%10%10 );
          }
       }
}

void getValue4(unsigned int pos){
     minnumber=999;
     while(minnumber){
          tempvalue1=minnumber*2889-1490616+2*(minnumber-999)*(minnumber-998);
          minnumber2=minnumber+1;
          tempvalue2=minnumber2*2889-1490616+2*(minnumber2-999)*(minnumber2-998);
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
       if(gap==0){
          printf("%dn",minnumber%10);
       }
       else if(gap <10){
             printf("%dn",gap);
       }else if(gap-9 <=180){
          calc1 = (gap-9)/2;
          if(((gap-9)%2)==0){
             printf("%dn",(calc1+9)%10);
           }else{
             printf("%dn",(calc1+10)/10);
          }
       }else if(gap-189<=2700){
          calc2 = (gap-189)/3;
          if(((gap-189)%3)==1){
             printf("%dn",(calc2+100)/100);
          }
          if(((gap-189)%3)==2){
             printf("%dn",((calc2+100)%100)/10);
          }
          if(((gap-189)%3)==0){
             printf("%dn",(calc2+99)%10 );
          }
       }else{
          calc3= (gap-2889)/4;
          if(((gap-2889)%4)==1){
             printf("%dn",(calc3+1000)/1000);
          }
          if(((gap-2889)%4)==2){
             printf("%dn",(calc3+1000)%1000/100);
          }
          if(((gap-2889)%4)==3){
             printf("%dn",(calc3+1000)%100/10);
          }
          if(((gap-2889)%4)==0){
             printf("%dn",(calc3+99)%10);
          }
       }
}

void getValue5(unsigned int pos){
     minnumber=9999;
     while(minnumber<33000){
           x=minnumber-9999;
           tempvalue1 = 189414495+38889*x + 5*(x+1)*x/2;
           minnumber2 = x+1;
          tempvalue2 = 38889*minnumber2 + 5*(minnumber2+1)*minnumber2/2+189414495;
          if((pos>=tempvalue1) && (pos<tempvalue2)){
              current = tempvalue1;
              gap=pos-tempvalue1;
              break;
          }
          minnumber++;
    }
       if(gap==0){
          printf("%dn",minnumber%10);
       }
       else if(gap <10){
             printf("%dn",gap);
       }else if(gap-9 <=180){
          calc1 = (gap-9)/2;
          if(((gap-9)%2)==0){
             printf("%dn",(calc1+9)%10);
           }else{
             printf("%dn",(calc1+10)/10);
          }
       }else if(gap-189<=2700){
          calc2 = (gap-189)/3;
          if(((gap-189)%3)==1){
             printf("%dn",(calc2+100)/100);
          }
          if(((gap-189)%3)==2){
             printf("%dn",((calc2+100)%100)/10);
          }
          if(((gap-189)%3)==0){
             printf("%dn",(calc2+99)%10 );
          }
       }else if(gap-2889<36000){
          calc3= (gap-2889)/4;
          if(((gap-2889)%4)==1){
             printf("%dn",(calc3+1000)/1000);
          }
          if(((gap-2889)%4)==2){
             printf("%dn",(calc3+1000)%1000/100);
          }
          if(((gap-2889)%4)==3){
             printf("%dn",(calc3+1000)%100/10);
          }
          if(((gap-2889)%4)==0){
             printf("%dn",(calc3+999)%10);
          }
       }
       else{
           calc3= (gap-38889)/5;
          if(((gap-38889)%5)==1){
             printf("%dn",(calc3+10000)/10000);
          }
          if(((gap-38889)%5)==2){
             printf("%dn",(calc3+10000)%10000/1000);
          }
          if(((gap-38889)%5)==3){
             printf("%dn",(calc3+10000)%1000/100);
          }
          if(((gap-38889)%5)==4){
             printf("%dn",(calc3+10000)%100/10);
          }

          if(((gap-38889)%5)==0){
             printf("%dn",(calc3+9999)%10);
          }
       }
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#else
#endif
scanf("%d",&num);
for(i=0;i<num;i++){
    scanf("%u",&position);
    if(position<=45){
       printf("%cn",pre45bits[position-1]);
    }else if(position<=9045){
        getValue2(position);
    }else if(position<=1395495){
        getValue3(position);
    }else if(position<=189414495){
        getValue4(position);
    }else{
        getValue5(position);
    }

}

#ifndef ONLINE_JUDGE
fclose(stdin);
#else
#endif
return 0;
}