1019. Number Sequence

来源:互联网 发布:java流程管理系统 编辑:程序博客网 时间:2024/06/15 21:38

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


代码

/*通过打表可以发现,一共可以分为31268个数据串。最长的一串有145236位*/#include <iostream>#include <math.h>#include <string.h>using namespace std;int len1[31270] = { 0 };    unsigned int len2[31270] = { 0 };     void group(){    int i = 0;    len1[1] = 1;    len2[1] = 1;    for (i = 2;i < 31270; i++)    {        len1[i] = len1[i - 1] + (int)log10(i*1.0) + 1;        len2[i] = len2[i - 1] + len1[i];    }}int main(){    int t = 0;    int j = 1,k = 1;    int MAX[145240] = { 0 };     group();    for (j = 1; j < 31270; j++)    {        char str[10];        int a = 0;        int temp;        temp = j;        while (temp)        {            str[a++] = temp % 10 + '0';            temp /= 10;        }             //确定最长的一串        while (a--)        {            MAX[k++] = str[a] - '0';        }    }    cin >> t;    while (t--)    {        int num = 0, i = 0;        cin >> i;        for (j = 0; j < 31270; j++)        {            if (len2[j] >= i)                break;        }        num = MAX[i - len2[j - 1]];        cout << num << endl;    }    return 0;}
原创粉丝点击