Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

1234.....n的这种序列,给定位数值,求该位数的数字的方法

Posted by Handle02 at 2013-11-26 20:07:48 on Problem 1019
//很有规律的,对于本题,求到万就可以
//关于pos的获得,可以使用2分,我直接求解了5个一元2次方程,不需要打表,直接迅速
// 仅供参考
void GetDigit(int pos)
{
                     if(pos<=9) printf("%d\n",pos);
                     else if(pos<=189)
                     {
                          if((pos-9)%2==0) printf("%d\n",((pos-9)/2-1)%10);
                          else printf("%d\n",(pos-9)/20+1);                          
                     }
                     else if(pos<=2889)
                     {
                          if((pos-189)%3==0) printf("%d\n",((pos-189)/3-1)%10);
                          else if((pos-189)%3==2) printf("%d\n",((pos-189)/30)%10);
                          else printf("%d\n",(pos-189)/300+1);
                          
                      }
                      else if(pos<=38889)
                      {
                          if((pos-2899)%4==0) printf("%d\n",((pos-2889)/4-1)%10);
                          else if((pos-2899)%4==3) printf("%d\n",((pos-2889)/40)%10);
                          else if((pos-2899)%4==2) printf("%d\n",((pos-2889)/400)%10);
                          else printf("%d\n",(pos-2889)/4000+1);
                       }
                       else 
                       {
                          if((pos-38889)%5==0) printf("%d\n",((pos-38889)/5-1)%10);
                          else if((pos-38889)%5==4) printf("%d\n",((pos-38889)/50)%10);
                          else if((pos-38889)%4==3) printf("%d\n",((pos-38889)/500)%10);
                          else if((pos-38889)%4==2) printf("%d\n",((pos-38889)/5000)%10);
                          else printf("%d\n",(pos-38889)/50000+1);
                       }
}
int main()
{
    double tmp=6050195089.0;
    int t;
    int num;
    int row;
    int m;
    int pos;
    while(scanf("%d",&t)!=EOF)
    {
       while(t--)
       {
           scanf("%d",&num);
           if(num<=45)
           {
              m=num;
              row=(int)((sqrt(8.0*m+1)-1)/2);
              pos=m-row*(row+1)/2;
              if(pos==0) printf("%d\n",row);
              else printf("%d\n",pos);
              
            }
           else if(num<=9045)
           {
                m=num-45;
                row=(int)(sqrt(m+25)-5);
                pos=m-row*row-10*row;
                if(pos==0)printf("%d\n",(row+9)%10);
                else GetDigit(pos);
     
            }
           else if(num<=1395495)
           {
                m=num-9045;
                row=(int)((sqrt(145161+24.0*m)-381)/6);
                pos=m-(3*row*row+381*row)/2;
                if(pos==0) printf("%d\n",(row+99)%10);
                else GetDigit(pos); 
                
            }
           else if(num<=189414495)
           {
                m=num-1395495;
                row=(int)((sqrt(8.0*m+8357881)-2891)/4);
                pos=m-2*row*row-2891*row;
                if(pos==0) printf("%d\n",(row+999)%10);
                else GetDigit(pos);
                
            }
           else
           {
               m=num-189414495;
               row=(int)((sqrt((double)(tmp +40.0*m))-77783)/10);
               pos=m-2.5*row*row-38891.5*row;
               if(pos==0) printf("%d\n",(row+9999)%10);
               else GetDigit(pos);
               
            }
        }
                              
       }
   return 0; 
 }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator