Number Sequence

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

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
//log10(n)就可以求出n的位数
#define LEN 32000
unsigned int a[LEN];
unsigned int b[LEN];
int main()
{
    a[0] = 0;
    a[1] = 1;
    b[0] = 0;
    b[1] = 1;
    int i;
    for(i = 2; i < LEN; i++)
    {
        a[i] = a[i-1] + log10((double)i) + 1;
        b[i] = b[i-1] + a[i];
    }

    unsigned int cases;
    unsigned int n;
    scanf("%u", &cases);
    while(cases--)
    {
        scanf("%u", &n);
        unsigned int j;
        for(j = 0; j < LEN; j++)
            if(n <= b[j])
                break;
        //在字串中的位置
        unsigned int remain = n - b[j-1];

        unsigned int value;
        unsigned int sum = 0;
        for(value = 1; value < LEN; value++)
        {
            sum += 1+log10((double)value);
            if(sum >= remain)
                break;
        }
        //value的位数
        unsigned int value_bits = 1+log10((double)value);
        unsigned int position = remain - (sum - value_bits);
        //求value的第position位
        char* str = (char*)malloc(10*sizeof(char));
        int temp = value;
        int stri = 0;
        while(temp)
        {
            int temp1 = temp%10;
            str[stri++] = temp1 + '0';
            temp = temp/10;
            
        }
        str[stri] = '';
        if(position == value_bits)
            printf("%c
", str[0]);
        else
            printf("%c
", str[position - 1]);
    }
    return 0;
}
 
选择了远方,便只顾风雨兼程
原文地址:https://www.cnblogs.com/ly-rabbit-wust/p/5575150.html