Find the nth digit

Source: Internet
Author: User

Find the nth digitTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 8801 Accepted Submission (s): 2504

problem DescriptionAssumptions:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
Now we're going to connect all the strings together.
S = 1121231234.......123456789123456789112345678912 .....
So can you tell me what the nth number is in the s string?

Inputthe input is first a number k, which represents a K-time inquiry.
The next K line has an integer n (1 <= n < 2^31) per line.
Outputfor each n, the nth corresponding number in the output s.

Sample Input
61234510

Sample Output
112124Problem Solving Ideas:First find the nth number in Si,This is achieved by the formula I (i+1)/2=n i= (2n+0.25) 0.5-0.5, where I should be rounded up instead of rounding down. Then the nth number is the number of n-si-1 in Si, and then mod 9 can be obtained results for data above S8 directly by checking the tableSource code:#include <stdio.h> #include <math.h> #include <stdlib.h>int main () {int k;    Double i;    unsigned long j,n;    int s[]={0,1,1,2,1,2,3,1,2,3,4,1,2,3,4,5,1,2,3,4,5,6,1,2,3,4,5,6,7, 1,2,3,4,5,6,7,8};    scanf ("%d", &k);      while (k--) {scanf ("%ld", &n);      if (n<=36) printf ("%d\n", S[n]);        else {I=ceil (sqrt (2*n*1.0+0.25)-0.5);        j= (unsigned long) i-1; n=n-j* (j+1)/2;// printf ("i=%lf\n j=%ld\n n1=%ld\n n2=%ld\n", i,j,j* (j+1)/2,n);        if (n%9) printf ("%d\n", n%9);       else printf ("9\n");    }} system ("Pause");    return 0; }

Find the nth digit

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.