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 ;}}