POJ 1019 Number Sequence

POJ 1019 Number Sequence

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

283Sample Output

22

解决方案:

对于这题,服务器空间,我们要用到math.h中的log10函数。在这里,我们有个规律,香港服务器,对于每一个正整数value,服务器空间,其位数等于log10(value) + 1.

我们用一个数组a来保存每个子串的位数(如子串123456789101112的位数为15).

我们用一个数组b来保存每个子串以及此子串之前所有的位数。

由于i有其取值范围,故我们可以选择数组维数大小为32000

下面就开始寻找特定位置上的那个数了。

1.首先根据所输入的位置来确定在哪个子串中。

2.确定是字串的哪个数。

3.再确定是该数的第几位。

4.然后输出之。

具体代码如下(gcc编译时加上-lm参数):

POJ 1019

1 #include <stdio.h> 2 #include <time.h> 3 #include <math.h> 4 #include <stdlib.h>LEN 32000 7 unsigned int a[LEN]; 8 unsigned int b[LEN]; 9 int main()10 {11a[0] = 0;12a[1] = 1;13b[0] = 0;14b[1] = 1;15int i;16for(i = 2; i < LEN; i++)17 {18a[i] = a[i-1] + log10((double)i) + 1;19b[i] = b[i-1] + a[i];20 }21 22unsigned int cases;23unsigned int n;, &cases);25while(cases–)26 {, &n);28unsigned int j;29for(j = 0; j < LEN; j++)30if(n <= b[j])31break;unsigned int remain = n – b[j-1];34 35unsigned int value;36unsigned int sum = 0;37for(value = 1; value < LEN; value++)38 {39sum += 1+log10((double)value);40if(sum >= remain)41break;42 }unsigned int value_bits = 1+log10((double)value);45unsigned int position = remain – (sum – value_bits);* str = (char*)malloc(10*sizeof(char));48int temp = value;49int stri = 0;50while(temp)51 {52int temp1 = temp%10;;54temp = temp/10;5556 };58if(position == value_bits), str[0]);printf(, str[position – 1]);62 };64 }

posted on

在乎的是沿途的风景以及看风景的心情,让心灵去旅行!

POJ 1019 Number Sequence

相关文章:

你感兴趣的文章:

标签云: