#include<iostream>
#include<fstream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const unsigned int MAX=2147483647;
int Num=100000;
int* len;
unsigned int *sum;
int input;
void init()
{
len[0]=0;
sum[0]=0;
char data[20];
int i;
for(i=1;sum[i-1]<=MAX;i++)
{
sprintf(data,"%d",i);
len[i]=len[i-1]+strlen(data);
sum[i]=sum[i-1]+len[i];
}
Num=i-1;
}
void solve()
{
int high=Num;
int low=0;
int mid;
while(low<=high)
{
mid=(high+low)/2;
if(sum[mid]==input)
break;
else if(sum[mid]<input)
low=mid+1;
else
high=mid-1;
}
if(sum[mid]==input)
{
char temp[20];
sprintf(temp,"%d",mid);
int temp_len=strlen(temp);
printf("%c\n",temp[temp_len-1]);
return ;
}
if(sum[low]>input)
low--;
int rest=input-sum[low];
high=low+1;
low=0;
while(low<=high)
{
mid=(high+low)/2;
if(len[mid]==rest)
break;
else if(len[mid]<rest)
low=mid+1;
else
high=mid-1;
}
if(rest==len[mid])
{
char temp[20];
sprintf(temp,"%d",mid);
int temp_len=strlen(temp);
printf("%c\n",temp[temp_len-1]);
return ;
}
if(len[low]>rest)
low--;
char temp[20];
sprintf(temp,"%d",low+1);
printf("%c\n",temp[rest-len[low]-1]);
}
int main()
{
len=new int[Num];
sum=new unsigned int[Num];
freopen("input.txt","r",stdin);
init();
int n;
scanf("%d",&n);
while(n--)
{
scanf("%d",&input);
solve();
}
return 0;
}
poj1019
最新推荐文章于 2013-07-11 14:23:55 发布