码迷,mamicode.com
首页 > 其他好文 > 详细

poj1019--Number Sequence(组合篇3)

时间:2015-01-22 15:31:18      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

Number Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 34582   Accepted: 9929

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

给出一个组合,1,12,123,1234,12345,123456,。。。。

找出第i个数字是什么,是数字,而不是第i个数,如第55是1,不是10

i的范围是2147483647,所以可以计算所组合最多不会超过10万个,可以遍历,找出第k个组合共有多少位,在累加出从1组合到k组合共多少位,这样就可以找出给出的i具体在哪个组合中,再遍历得到它

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
#define INF 2147483647
LL num[100000] , sum , cnt ;
int k[6] = {0,9,99,999,9999,99999} ;
int digit[10] ;
void init()
{
    int i = 0 , j ;
    memset(num,0,sizeof(num)) ;
    while( num[i] <= INF )
    {
        i++ ;
        sum = 0 ;
        for(j = 1 ; j < 6 ; j++)
        {
            if( i > k[j] )
            {
                sum += ( k[j] - k[j-1] )*j ;
            }
            else
                break ;
        }
        sum += (i-k[j-1])*j ;
        num[i] = sum + num[i-1] ;
    }
    cnt = i ;
    return ;
}
LL f(LL temp)
{
    LL m = 0 ;
    while( temp )
    {
        digit[++m] = temp%10 ;
        temp /= 10 ;
    }
    return m ;
}
int main()
{
    init() ;
    LL t , n , i , cnt ;
    scanf("%I64d", &t) ;
    while( t-- )
    {
        scanf("%I64d", &n) ;
        i = 0 ;
        while( n > num[i] )
        {
            i++ ;
        }
        n -= num[i-1] ;
        i = 1 ;
        while( 1 )
        {
            cnt = f(i) ;
            if( n > cnt )
            {
                n -= f(i) ;
                i++ ;
            }
            else
                break ;
        }
        printf("%d\n", digit[cnt+1-n]);

    }
    return 0;
}

poj1019--Number Sequence(组合篇3)

标签:

原文地址:http://blog.csdn.net/winddreams/article/details/43019271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!