poj 1019 Number Sequence

来源:互联网 发布:sql 2008 r2 64位下载 编辑:程序博客网 时间:2024/04/20 07:59

http://poj.org/problem?id=1019

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

 

 

题意:

有一个这样的数列

1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1324567891011 123456789101112 …(空格是我自己加上去区分的)

然后问你第n位数是什么,注意,找的是那一位数比如加黄色的这一位就是1了

 

 

模拟题来的,主要是知道这个求位数的公式

数列i(1234567…i)所占的长度
dig[i] = dig[i - 1] + (int)log10((double)i) + 1;

自己想了好久没想出来,真是什么都还给数学老师了

在网上找了好过代码出来看下

才知道有公式的囧

mark,什么时候才能自己推断出这样的公式啊…


#include<stdio.h>
#include<math.h>
#include<cstdio>

//用64位整形范围才够大,也能用unsigned int

__int64 bit[100010];//bit[i]记录i(1234567…i)所占的长度
__int64 len[100010];//len[i]记录i到达的长度

int main()
{
 int i;
 bit[1] = len[1] = 1;
 for (i = 2; i < 100001; i++)
 {
  bit[i] = bit[i - 1] + (int)log10((double)i) + 1;
  len[i] = len[i -1] + bit[i];
 }
 
 int t,n;
 int n_temp;//在该序列的第几个
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d",&n);
  for(i=1;len[i]<n;i++);
 
  n_temp=n-len[i-1];
 
 
  //用之前的算位数的方法算到达这一数列的哪个树
 
  int length=0;
  for(i=1;n_temp>length;++i)
  {
   length=bit[i];//之前算过了
  }
 
 
  printf("%d/n",(i-1)/(int)(pow((double)10,length-n_temp))%10);
 }
 
 return 0;
}

原创粉丝点击