poj 1019 Number Sequence

// 题意: 一串有规律的数字,
// 11212312341234512345612345671234567812345678912345678910123456789101112345678910...
// 拆分出来就是 11 212 3123 41234 5… 问第i位数字是多少,i ≤ 2147483647

#include <iostream>
#include <string.h>
using namespace std;
int count[35000];
int digit(int n) //返回n的位数
{
int d = 0;
while(n)
{
n /= 10;
d++;
}
return d;
}
int main()
{
int cases,i, j,ans;
long long n;
for (i = 1; i < 35000; i++)
count[i] = count[i - 1] + digit(i);
cin>>cases;
while(cases--)
{
cin>>n;
i = 0;
while (n >= count[i])
{
n -= count[i];
i++;
}
if (n==0)
ans= (i - 1) % 10;
else
{
i = 0;
while(count[i] <= n)
i++;
n -= count[i - 1]; //count[i-1]<=n<count[i]
if (n==0)
ans = (i - 1) % 10;
else
{
j = digit(i);
while(n++ < j)
{
i /= 10;
}
ans = i % 10;
}
}
cout<<ans<<endl;
}
return 0;
}

posted on 2011-07-17 23:36  sysu_mjc  阅读(138)  评论(0编辑  收藏  举报

导航