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.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 should be one output line per test case containing the digit located in the position i.Sample Input
2 8 3
Sample Output
2 2
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest題目大意就是給你這一串數字11212312341234512345612345671234567812345678912345678910123456789101112345678910……(未列完)
要我們求出第n個數是多少(從左到右看),例如第2個是1,第三個是2,第八個是2;
如果仔細觀察這一串數字,可以發現他可以還分為很多小串,假設第i小串是123……i,假設第i小串所占的空間是a[i],則通過對比a[i]與a[i+1]發現,
第i+1串只比第i串多一個數,即i+1,故他們所占的空間差就是第i+1所占的空間。
對任意一個數所占的空間很好求,即 (int)log10(k)+1;
然後就可以求出每一個串的起始位置,通過與n比較就可以確定n出現在那一個串裡,最後在求出n在這個串裡的相對位置,就可以求出該題的解
#include#include #include #include #include #include