1019 Number Sequence

Number Sequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22588 Accepted: 6019

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 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

 

#include<cstdio> #include<cmath> unsigned int sum[31270],f[31270]; int work(int n) { int i=1; int length=0; /* 找到 n 所在的组 */ while(sum[i]<n) i++; /* n 在该组的下标 */ int pos=n-sum[i-1]; /* length: n指向的数字的最后一位的下标 */ for(i=1;length<pos;i++) { length+=(int)log10((double)i)+1; } /* 去掉所求位后面的数字然后取余 */ /* i: n指向的数字 + 1 */ return((i-1)/(int)pow((double)10,length-pos))%10; } int main() { f[1]=1; sum[1]=1; for(int i=2;i<31270;i++) { f[i]=f[i-1]+(int)log10((double)i)+1; sum[i]=sum[i-1]+f[i]; } int cas; scanf("%d",&cas); while(cas--) { int n; scanf("%d",&n); printf("%d/n",work(n)); } return 0; }

你可能感兴趣的:(Integer,input,each,internet,output,Numbers)