POj 1019 number sequence(数学)

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

题目要求求的是i位置上的数 只有0到9 因为是这个i指的是位数里面位,利用数学公式 位数=(int)log10((double)i)+1
思路先用一个数组a[]求每个数的位数 再用sum[]存每个串的位数
找出n在哪个串
再找出n在哪个串的哪个位置 pos
一个for循环j用十进制递增求每个j的位数累加,一直加到大于等于pos,由于这个j然后用j化为位数,由于j还要表示位数为0的数 所以是j-1,然后去掉所求位后面的位数再模10得出结果

#include 
#include 
#include 
#include 
using namespace std;
#define max 31270
typedef long long LL;
unsigned int a[max];
unsigned int s[max];
void start()
{
    int i,j;
    a[1]=1;   //位数从1开始
    s[1]=1;
    for(i=2;i<31270;i++)
    {
        a[i]=a[i-1]+(int)log10((double)i)+1; //得到第i个数的位数
        s[i]=s[i-1]+a[i];               //得到这个字符串的位数
    }
}
int res(int n)
{
    int i=1,j=1;
    int m=0;
    int len=0;
    for(i=1;i<31270;i++)
    {
        if(s[i]>=n)          //n代表要求的位
            break;           //先找到n在的字符串
    }
    int pos=n-s[i-1];       //n在该组的下标
    for(j=1;lenint)log10((double)j)+1; //len:n指向的数字的最后一位的下标
    }                                 
    return ((j-1)/(int)pow(10.0,len-pos))%10;//因为pos=n-s[i-1]
                            //去掉所求位后面的位数取余
                            //j:n指向的数字+1 因为位上可以有0,相当于已知十进制数和这个位从右往左数的位数,求这个位上的数。很好想。
}
int main()
{
    int t;
    int te;
    cin>>t;
    start();
    while(t--)
    {
        cin>>te;
        cout<

你可能感兴趣的:(POj 1019 number sequence(数学))