UVa 10706:number Sequence

Source: Internet
Author: User

Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1647

Original title:

A single Positive integer i is given. Write a program to find the digit located in the position I in the sequence of number groups s1s2 ... Sk. Each group Sk consists of a sequence of positive integers numbers ranging from 1 to K, written one after another. For example, the 80digits of the sequence are as follows:

11212312341234512345612345671234567812345678912345678910123456789101112345678910

Input

The ' I ' input file contains a single integer t (1 <=t <=25), the number of test cases, followed by one Line for each test case. The line for a test case contains the single integer I (1 <=i <=2147483647)

Output

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

There should be one output line per test case containing the digit located in the position I.

Sample input:

2

8

3

Sample output:

2

2

The main effect of the topic:

A sequence of numbers by s1s2 ...   SK composition, wherein SI also represents a sequence, for 1,2,3....i. Enter a number I and then output the number on the first digit of the sequence.

Analysis and Summary:

Too bad, only think of the simulation + Find the Law method.

First you can find some rules:

The start number of each digit is 1,10,100,1000,10000 ... You can take these starting numbers as a dividing line.

Saved with an array of num, num "i" is the sum of the digits as 1,2...i.

Then use this array to find the total number of a SI sequence is very good to do, assuming i=101, then SI is the number sequence 1,2 ... 101, its numeric digits sum si= (i-digit+1) *+num[digit-1], where digit represents the number of digits of the I.

Then open an array s,s "K" to save the s1s2 ... SK The total number of this sequence.

After preprocessing this information, you can start to find and simulate.

To find the number on the I-bit, then first find out which si it belongs to, you can directly in the S-array of binary lookup lower bound, that is, to find the first >=i. Suppose that the subscript to find is K, if s[k] just equals I, then k this number is just the last number of SK sequence 1,2,3......K, then k%10 is the answer.

If arr[k]>i, then you can know that I belong to the SI this subsequence between a number. Can continue to be counted as m=i-s "K-1", then the number of the answer that digit is in SI in the first m bit.

The next step is to simulate and find out how much the SI is in the first M.

You can further narrow the lookup range according to the NUM array, find the first subscript of the >=m in the NUM array, assuming that the subscript is P, and if num "P" ==m, indicating that the number of digits is exactly at the end of the line, it must be 9.

If num "P" >m, then the number of digits in the table above can be calculated in which range, and then the simulation in that range to continue until M bit. Find out which element of the number I digit belongs to the SI sequence 1,2,3......i. The specific or look at the code, the text is really not very good expression.

Code:

* * * Uva:10706-number Sequence * time:0.008s * author:d_double * * * * * * * #include <iostream> #include  
;cstdio> #include <algorithm> #define MAXN 31272 using namespace std;  
Long Long S[MAXN];  
int scale[9]={0,1,10,100,1000,10000,100000,1000000,10000000};  
      
      
int num[9]={0,9,90,900,9000,90000,900000,9000000,90000000};  
    inline int getdigit (int n) {int cnt=0;  
        while (n>0) {++cnt;  
    n/= 10;  
} return CNT;  
    } inline void init () {for (int i=2; i<9; ++i) {num[i] = num[i-1]+num[i]*i; //Calculate the S1,S2,S3 ... S5//And find out their and s[i]=s1+s2+.  
    +si s[0]=0;  
        for (int i=1; i<maxn; ++i) {int digit=getdigit (i);  
        S[i] = (i-scale[digit]+1) *digit+num[digit-1];  
    S[i] + = s[i-1];  
    int main () {init ();  
    int T, n;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d", &n); Long long *P=lower_bound (S, S+MAXN, N);  
        if (*p==n) printf ("%d\n", (p-s)%10);   
            else{int m=n-* (P-1);  
            BOOL Ok=false;  
            Int J;  
                    For (j=0 j<9; ++j) {if (num[j]==m) {ok=true;  
                Break  
                else if (num[j]>m) {break;  
                } if (OK) {printf ("9\n");  
            Continue int left=m-num[j-1];  
            How many times to int cnt=j;  
            int k;  
            for (K=scale[j]; cnt<left; cnt + j,++k);;  
            int time=cnt-left;  
            while (time>0) {k/=10; time--}  
        printf ("%d\n", k%10);  
} return 0; }

Author: csdn Blog shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.