#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <deque>
#include <limits>
#include <cmath>
using namespace std;
//求10进制正整数k的位数,如123为3位
int nobits(int k)
{
int count = 0,t = k;
while(t > 0)
{
t /= 10;
count++;
}
return count;
}
//求10进制正整数数k的第n位数,例如1234的第一位为1,第二位为2...
int digit(int k,int n)//k>=1, n >= 1
{
deque<int> intd;
while(k > 0)
{
intd.push_front(k%10);
k /= 10;
}
return intd[n-1];
}
//连续输出位数,令k = 20,则输出格式如下:123456789 10 11 12 13 14 15 16 17 18 19 20
int nodigits(int k)//num of output digits
{
int sum = 0;
int count = nobits(k);
int t = 9;
for(int i = 1; i < count; i++)
{
sum += t*i;
t *= 10;
}
sum += count*(k - (int)pow(10.0,count-1) + 1);
return sum;
}
//求连续输出的后第n位数,例如,k = 20,n = 16,返回1;n = 17,则返回值为3
int r(int k, int n)//第n位数
{
int count = nobits(k);
int sum = 0;
int i,j,t = 9;
for(i = 1; i <= count; i++)
{
if(sum + t*i >= n)break;
sum += t*i;
t *= 10;
}
t = (int)(pow(10.0,i-1));
j = ceil((n - sum)*1.0/i);
int tk = t + j - 1;
int tn = (j - 1)*i + sum;
return digit(tk,n - tn);
}
int result(int n)
{
int k;
long long sum = 0;
for(k = 1;; ++k)
{
if(sum+nodigits(k) >= n)break;
sum += nodigits(k);
}
return r(k,n-sum);
}
int main()
{
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
/*cout<<numeric_limits<long int>::max()<<endl;
cout<<numeric_limits<long long>::max()<<endl;
int count = nodigits(12345);
for(int i = 1; i <= count; i++)
cout<<r(12345,i)<<endl;
cout<<endl;*/
//cout<<nodigits(99)<<endl;
int t; // 1 <= t <= 10
int num;
cin>>t;
while(t--)
{
cin>>num;
cout<<result(num)<<endl;
}
return 1;
}
pku acm 1019
最新推荐文章于 2019-09-06 20:29:07 发布