POJ 1019 Number Sequence(水~)

来源:互联网 发布:2017双十一实时数据 编辑:程序博客网 时间:2024/05/15 06:12

Description
一个数字序列,规律是这样的:
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112……
求这个序列中的第n位是什么数字
Input
第一行为一个整数T表示用例组数,每组用例占一行为一个整数n
Output
对于每组用例,输出这个数字序列中第n位的数字
Sample Input
2
8
3
Sample Output
2
2
Solution
简单题,首先统计每个数字占的位数cnt[i]=(int)log10(i)+1,然后统计每个1~i组成的串占的位数sum[i]=cnt[1]+…+cnt[i],预处理出这些之后,对于每个n,首先通过不断减sum[i]得知第n位处于哪一个1~m之间,然后再不断减cnt[i]得知第n位位于1~m中哪一个数中,然后问题就变成找到一个数字temp的第n位,这个可以先将从左往右数的第n位转化为从右往左数的第(int)log10(temp*1.0)+1-n位,那么ans=temp/(int)pow(10.0,1.0*n)%10
Code

#include<stdio.h>#include<string.h>#include<math.h>#define maxn 33333typedef long long ll;ll cnt[maxn],num[maxn],n;int t,ans;void init(){    memset(cnt,0,sizeof(cnt));    memset(num,0,sizeof(num));    for(int i=1;i<maxn;i++)    {        cnt[i]=cnt[i-1]+(int)log10(1.0*i)+1;        num[i]=num[i-1]+cnt[i];    }}int main(){    init();    scanf("%d",&t);    while(t--)    {        scanf("%lld",&n);        int temp=1;        while(n>num[temp])temp++;        n-=num[temp-1];        temp=1;        while(n>(int)log10(temp*1.0)+1)n-=((int)log10(temp*1.0)+1),temp++;        n=(int)log10(temp*1.0)+1-n;        ans=temp/(int)pow(10.0,1.0*n)%10;        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击