Number Sequence

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

题意:按照11212312341234561234567.。。123.。。。

    题意很简单,但是需要处理当数字大于9时,每一位仅算一个数字;

    使用数组a存每一组中有的个数,再用数组s存前i组的总个数;a[ i ] = a[ i - 1 ] + log10( i ) + 1 ;  其中log10( i )就是当数值大于9时所多出来的位数,例如12,就代表1、2;

    然后处理时,直接找到输入的n位于第i组,然后找出n位于该组中的位置,最后使用(i -  1 ) / 10 ^ ( 多出来的位数) % 10 ;因为每一个位置,都是0~9

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<ctime>
#include<deque>
#include<stack>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<numeric>
#include<sstream>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional>

using namespace std ;
const int maxn = 31270 ;

unsigned int a[ maxn ] , s[ maxn ] ;
void Union()
{
	a[ 1 ] = 1 ;
	s[ 1 ] = 1 ;
	for( int i = 2 ; i < maxn ; ++i )
	{
		a[ i ] = a[ i - 1 ] + ( int )log10( ( double ) i ) + 1 ;
		s[ i ] = s[ i - 1 ] + a[ i ] ;
	}
}

int work( int n )
{
	int len = 0 , temp , pos , i = 1 ;
	while( s[ i ] < n )	
		++i ;
	pos = n - s[ i - 1 ] ;
	for( i = 1 ; len < pos ; ++i )
		len += ( int )log10( ( double ) i ) + 1 ;
	return ( ( i - 1 ) / ( int ) pow( ( double )10 , len - pos ) ) % 10 ;
}

int main()
{
	int Case , n ;
	Union() ;
	cin >> Case ;
	while( Case-- )
	{
		cin >> n ;
		cout << work( n ) << endl ;
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值