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
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 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
大意:从1开始排序,各个串为,1,12,123,1234,.....,12345678910,现在输入一个下标,让你输出这个下标的数是多少.
我们先开两个数组,一个数组存以该数为最大值的串其中有a[i]个数,另一个存以该数为最大值的串的最开始数的下标b[i],那么输入该m,i从1开始,t+=a[i],如果超过了,记录这个i值,即最大值,再把超过的减去原来的m就是该数的现在的第几位,那么只要输出该数除以有的位数再取余就是所求。
#include<cstdio> #include<iostream> #include<cmath> using namespace std; long long a[50000],b[50000]; long long i,t,m,tt; long long pos ,ans; int main() { a[1] = 1; b[1] = 1; for( i = 2; i < 38000; i++){ a[i] = a[i-1] + (int)log10((double)i) + 1;//数目 b[i] = b[i-1]+a[i];} cin>>tt; while(tt--){ cin >> m; i = 1; while(m > b[i]){ i++; } pos = m - b[i-1];//据起始点的位数 t = 0; for( i = 1;t < pos;i++) { t+=(int)log10((double)(i))+1; }//t为总数 pos = t - pos;//pos就为该数的位数 i--;//i为这个数 ans = i/(int)pow(10.0,(double)pos)%10; printf("%lld ",ans); } return 0; }