Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31455 | Accepted: 8924 |
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:
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 12345678910
For example, the first 80 digits of the sequence are as follows:
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 12345678910
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
题意:已知题目中按一定规律排列的序列,输入i求第i个位置上对应的数字;
若干个小时的成果,记录一下思考过程。
思路: 将前2147483647个数分为5个区,
第i个区 区间范围(num) 前num个数序列个数总和
1 1——9 45
2 10——99 9000
3 100——999 1386450
4 1000——9999 188019000
5 10000——99999 2147483647
即第i个区有i位数;
再找第i个区的第j个数
第i区的第一个数 该数对应序列的长度
1 1
10 11
100 192
1000 2893
10000 38894
再找第j个数的第k段
第k段 第k段包含的序列总长度
1 9
2 180
3 2700
4 36000
5 450000
找到第k段后就可以确定第i个数对应的十进制数了,例如求第80个位置时,它在第2个区的第3个数(十进制是12)的第2段,就可以确定第80个数对应的十进制数是10,然后输出第80位数字。
1 #include<stdio.h> 2 #include<string.h> 3 int x; 4 int seq_sum[] = {0,45,9000,1386450,188019000,2147483647}; 5 int seq_len[] = {0,1,11,192,2893,38894}; 6 int seq_num[] = {0,1,10,100,1000,10000}; 7 int seq_part[] = {0,9,180,2700,36000,450000}; 8 9 void solve() 10 { 11 int i,j,k,t,asi,s; 12 for(i = 1; ; i++)//第i区; 13 { 14 if(x > seq_sum[i]) 15 x = x-seq_sum[i]; 16 else break; 17 } 18 19 for(j = 1; ; j++)//第j个数; 20 { 21 int tmp = seq_len[i]+(j-1)*i; 22 if(x > tmp) 23 x = x-tmp; 24 else break; 25 } 26 27 28 for(k = 1; ; k++)//对应第k段; 29 { 30 if(x > seq_part[k]) 31 x = x-seq_part[k]; 32 else break; 33 } 34 35 t = x/k; 36 asi = x%k; 37 38 if(asi != 0) t++; 39 else asi = k; 40 41 s = seq_num[k]+t-1; 42 43 for(int l = 1; l <= k-asi; l++) 44 s/=10; 45 printf("%d ",s%10); 46 } 47 48 int main() 49 { 50 int test; 51 scanf("%d",&test); 52 while(test--) 53 { 54 scanf("%d",&x); 55 solve(); 56 } 57 return 0; 58 }