POJ 1019

自己找规律做的,枚举了所有的情况

//11338043	c00h00g	1019	Accepted	396K	0MS	G++	5263B	2013-03-11 19:46:22
#include<stdio.h>
#include<stdlib.h>

long long t,num;
int main(){
    scanf("%I64d",&t);
    while(t--){
        scanf("%I64d",&num);
        long long i,sum=0;
        if(num<=45){
            for(i=1;;i++){
                sum+=i;
                if(sum>=num)
                    break;
            }
            long long pre=sum-i;
            num-=pre;
            printf("%I64d\n",num);
        }else if(num>45&&num<=9000+45){
            num=num-45;
            for(i=1;;i++){
                sum+=(9+i*2);
                if(sum>=num)
                    break;
            }
            long long pre=sum-9-i*2;
            //num表示位数 
            num-=pre;
            if(num<10)
                printf("%I64d\n",num);
            else{
                //第几个数
                long long index=(num-9)%2==0?(num-9)/2:(num-9)/2+1;
                index+=9;
                if((num-9)%2==0) printf("%I64d\n",index%10);
                else printf("%I64d\n",index/10);
            }
        }else if(num>9000+45&&num<=9000+45+1386450){
            num=num-45-9000;
            for(i=1;;i++){
                sum+=(189+3*i);
                if(sum>=num)
                    break;
            }
            long long pre=sum-189-3*i;
            num-=pre;
            if(num<10)
                printf("%I64d\n",num);
            else if(num>=10&&num<190){
                long long index=(num-9)%2==0?(num-9)/2:(num-9)/2+1;
                index+=9;
                if((num-9)%2==0) printf("%I64d\n",index%10);
                else printf("%I64d\n",index/10);
            }
            else{
                long long index=(num-189)%3==0?(num-189)/3:(num-189)/3+1;
                index+=99;
                if((num-189)%3==1) printf("%I64d\n",index/100);
                else if((num-189)%3==2) printf("%I64d\n",(index/10)%10);
                else printf("%I64d\n",index%10);
            }
        }
        else if(num>9000+45+1386450&&num<=9000+45+1386450+188019000){
            num=num-45-9000-1386450;
            for(i=1;;i++){
                sum+=2889+4*i;
                if(sum>=num)
                    break;
            }
            long long pre=sum-2889-4*i;
            num-=pre;
            if(num<10)
                printf("%I64d\n",num);
            else if(num>=10&&num<190){
                long long index=(num-9)%2==0?(num-9)/2:(num-9)/2+1;
                index+=9;
                if((num-9)%2==0) printf("%I64d\n",index%10);
                else printf("%I64d\n",index/10);
            }
            else if(num>=190&&num<2890){
                long long index=(num-189)%3==0?(num-189)/3:(num-189)/3+1;
                index+=99;
                if((num-189)%3==1) printf("%I64d\n",index/100);
                else if((num-189)%3==2) printf("%I64d\n",(index/10)%10);
                else printf("%I64d\n",index%10);
            }else{
                long long index=(num-2889)%4==0?(num-2889)/4:(num-2889)/4+1;
                index+=999;
                if((num-2889)%4==1) printf("%I64d\n",index/1000);
                else if((num-2889)%4==2) printf("%I64d\n",(index/100)%10);
                else if((num-2889)%4==3) printf("%I64d\n",(index%100)/10);
                else printf("%I64d\n",index%10);
            }
        }
        else{
             num=num-45-9000-1386450-188019000;
             for(i=1;;i++){
                sum+=38889+5*i;
                if(sum>=num)
                    break;
            }
            long long pre=sum-38889-5*i;
            num-=pre;
            if(num<10)
                printf("%I64d\n",num);
            else if(num>=10&&num<190){
                long long index=(num-9)%2==0?(num-9)/2:(num-9)/2+1;
                index+=9;
                if((num-9)%2==0) printf("%I64d\n",index%10);
                else printf("%I64d\n",index/10);
            }
            else if(num>=190&&num<2890){
                long long index=(num-189)%3==0?(num-189)/3:(num-189)/3+1;
                index+=99;
                if((num-189)%3==1) printf("%I64d\n",index/100);
                else if((num-189)%3==2) printf("%I64d\n",(index/10)%10);
                else printf("%I64d\n",index%10);
            }else if(num>=2890&&num<38890){
                long long index=(num-2889)%4==0?(num-2889)/4:(num-2889)/4+1;
                index+=999;
                if((num-2889)%4==1) printf("%I64d\n",index/1000);
                else if((num-2889)%4==2) printf("%I64d\n",(index/100)%10);
                else if((num-2889)%4==3) printf("%I64d\n",(index%100)/10);
                else printf("%I64d\n",index%10);
            }else{
                long long index=(num-38889)%5==0?(num-38889)/5:(num-38889)/5+1;
                index+=9999;
                if((num-38889)%5==1) printf("%I64d\n",index/10000);
                else if((num-38889)%5==2) printf("%I64d\n",(index/1000)%10);
                else if((num-38889)%5==3) printf("%I64d\n",(index/100)%10);
                else if((num-38889)%5==4) printf("%I64d\n",(index%100)/10);
                else printf("%I64d\n",index%10);
            }
        }
    }
    //system("pause");
    return 0;
} 


你可能感兴趣的:(POJ 1019)