10706-number Sequence
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem &problem=1647
http://poj.org/problem?id=1019
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 integers numbers ranging from 1 to K, written one after another.
For example, the digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The ' 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, Iran Nationwide Internet programming Contest
Complete code:
Complexity: O (n), but the constant term is small
/*uva:0.016s*/
/*poj:0ms,648kb*/
#include <cstdio>
#include <cmath>
int a[31270], s[ 31270];
inline int pow_10 (int x, int d)
{while
(d--) x/=;
return x%;
}
int main (void)
{
int t, N, T, I;
for (i = 1;; i++)
{
A[i] = a[i-1] + (int) (LOG10 (double) i) + 1;
S[i] = S[i-1] + a[i];
if (S[i] < 0) break;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
i = 0;
while (S[i] >= 0 && s[i] < n) i++;///n the group
t = n-s[i-1];
i = 0;
while (A[i] < T) i++;///n the single-digit number of digits
printf ("%d\n", pow_10 (i, a[i)-T);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/