10706

开始看错题了,以为是求第n个数为多少,实际应该是求第n位的数字是几,是一个0~9的数字,

我用两个数组存,sum[i]为最大数为i时其前面有多少位数,f[i]表示最大为i时1~i共占了多少位

//============================================================================
// Name        : 10706.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int N, t, t1;
long long n, ans;
char s[20];
long long f[70000], sum[70000], len[70000];

int main()
{
	freopen("a.txt", "r", stdin);
	scanf("%d", &N);
	f[1] = 1;
	sum[1] = 1;
	for(int i = 2;i < 70000;i++)
	{
		t1 = i;
		t = 0;
		while(t1)
		{
			t++;
			t1/=10;
		}
		len[i] = t;
		f[i] = f[i-1]+t;
		sum[i] = sum[i-1] + f[i];
	}
	while(N--)
	{
		scanf("%lld", &n);
		t = 0;
		while(sum[t] < n)
		{
			t++;
		}
		n -= sum[t-1];
		for(int i = 1;i <= t+1;i++)
		{
			if(f[i] > n)
			{
				n -= f[i-1];
				t1 = i;
				break;
			}
		}
		if(n == 0) printf("%d\n", (t1-1)%10);
		else
		{
			sprintf(s, "%d", t1);
			t = strlen(s)-n;
			t1/=pow(10, t);
			printf("%d\n", t1%10);
		}
	}
	return 0;
}

posted @ 2011-05-17 21:41  KOKO's  阅读(322)  评论(0)    收藏  举报