Hdu1597 find the nth Digit

Source: Internet
Author: User
Problem description hypothesis:
S1 = 1
S2 = 12
S3. = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11= 12345678912
............
S18= 123456789123456789
..................
Now we connect all strings
S = 1121231234 ...... 123456789123456789112345678912 .........
Can you tell me the nth number in the S string?


The input is a number K, which indicates K queries.
Each row in the next K Rows has an integer N (1 <=n <2 ^ 31 ).


For each n, output the nth number in S.


Sample Input

 
61234510


Sample output

 
112124
// I * (I + 1)/2 = N, I can be obtained, and then I % 9, you can get the answer # include <iostream> # include <cmath> using namespace STD; int main () {double N ;__ int64 I, j, T, Y; scanf ("% i64d", & T); While (t --) {scanf ("% lf", & N ); double X = SQRT (2 * n * 1.0 + 0.25)-0.5; // I * (I + 1)/2 = N-> (I * I + I + 0.25) = 2 * n-0.25-> (I + 0.5) ^ 2 = (2 * n-0.25) if (x> (INT) X/1) y = (INT) x/1 + 1; else y = (INT) X/1; // rounded up because I * (I-1) needs to be satisfied) /2 <n <I * (I + 1)/2, n is in the sum (I-1) -- sum (I), and then find the remainder int sum = y * (Y-1) /2; y = N-sum; If (Y % 9 = 0) cout <"9"; else cout <Y % 9; cout <Endl ;}}

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.