风言枫语  

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;
}


 

 

posted on 2013-09-04 17:56  风言枫语  阅读(345)  评论(0)    收藏  举报