poj Number Sequence

来源:互联网 发布:搞笑 知乎 编辑:程序博客网 时间:2024/06/07 18:22
Number Sequence (排列组合)
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu
Submit Status

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

283

Sample Output

22

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cmath>  
  4. #define max 31270  
  5. using namespace std;  
  6. unsigned int a[max];  
  7. unsigned int s[max];  
  8. void start()  
  9. {  
  10.     int i,j;  
  11.     a[1]=1;  
  12.     s[1]=1;  
  13.     for(i=2; i<31270; i++) //打表记录总的位数  
  14.     {  
  15.         a[i]=a[i-1]+(int)log10((double)i)+1;  
  16.         s[i]=s[i-1]+a[i];  
  17.     }  
  18. }  
  19. int res(int n)//预处理,不断缩小范围  
  20. {  
  21.     int i=1,j=1;  
  22.     int m=0;  
  23.     int len=0;  
  24.   
  25.     for(i=1; i<31270; i++)  
  26.     {  
  27.         if(s[i]>=n)  
  28.           break;  
  29.     }  
  30.     int pos=n-s[i-1];  
  31.     for(j=1; len<pos; j++)  
  32.     {  
  33.         len+=(int)log10((double)j)+1;  
  34.     }  
  35.     return ((j-1)/(int)pow(10.0,len-pos))%10;  
  36. }  
  37. int main()  
  38. {  
  39.     int t;  
  40.     int test;  
  41.     cin>>t;  
  42.     start();  
  43.     while(t--)  
  44.     {  
  45.         cin>>test;  
  46.         cout<<res(test)<<endl;  
  47.     }  
  48.     return 0;  
  49. }

0 0
原创粉丝点击