POJ1019: Number Sequence
Description
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 integer numbers ranging from 1 to k, written one after another.
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), 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
There shoshould be one output line per test case containing the digit located in the position I.
Sample Input
283
Sample Output
22
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest
The question is to give you this string of numbers 11212312341234512345612345671234567812345678912345678910123456789101112345678910 ...... (Not listed)
Calculate the number of nth numbers (from left to right). For example, 2nd is 1, the third is 2, and the eighth is 2;
If you carefully observe this number, you can find that it can be divided into many small strings. Suppose the I-th small string is 123 ...... I. If the space occupied by the I-th small string is a [I], we can compare a [I] with a [I + 1] and find that,
The I + 1 string only has a number more than the I + 1 string, so the space difference they occupy is the space occupied by I + 1.
It is very helpful for the space occupied by any number, that is, (int) log10 (k) + 1;
Then we can find the starting position of each string. By comparing with n, we can determine where n appears in that string. Finally, we can find the relative position of n in this string, you can find the answer to this question.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define ls 2 * I # define rs 2 * I + 1 # define up (I, x, y) for (I = x; I <= y; I ++) # define down (I, x, y) for (I = x; I> = y; I --) # define mem (a, x) memset (, x, sizeof (a) # define w (a) while (a) # define LL long longconst double pi = acos (-1.0 ); # define N 32000 # define mod 19999997 # define INF 0x3f3f3f3f # define exp 1e-8int a [40000], num [1000000], l; LL s [40000]; int main () {int t, n, I, j, k; a [0] = 0; a [1] = 1; up (I, 2, N) {a [I] = a [I-1] + (int) log10 (1.0 * I) + 1; // plus the number of digits occupied by each number, obtain the length of string I} s [0] = 1; up (I, 1, N) // obtain the starting coordinate of the I-th string {s [I] = s [I-1] + a [I-1];} l = 1; up (I, 1, N) // calculate the longest string {int bit [50]; t = I; int len = 0; w (t) {int r = t % 10; bit [len ++] = r; t/= 10;} w (len --) {num [l ++] = bit [len] ;}} scanf ("% d", & t); w (t --) {scanf ("% d", & n); up (I, 1, N) {if (s [I]> = n) break;} if (s [I] = n) // It is the start of a string printf ("1 \ n "); else // locate position printf ("% d \ n", num [n-s [I-1] + 1]);} return 0 ;}