Find the nth Digit
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 8351 accepted submission (s): 2381
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
#include<cstdio>#include<string>#include<cmath>#include<cstring>#include<cctype>#include<cstdlib>#include<vector>#include<set>#include<map>#include<stack>#include<queue>#include<list>#include<algorithm>#include<iostream>#include<sstream>using namespace std;int main(){ int cas; int n; int sum; scanf("%d", &cas); while( cas-- ) { scanf("%d", &n); sum = 0; for(int i=1; ; i++) { sum += i; if( sum >= n ) { sum -= i; break; } } int ans = n - sum; if( ans % 9 == 0 ) printf("9\n"); else printf("%d\n", ans%9); } return 0;}