Number Sequence POJ - 1019 递推 数学

  题意 1 12 123 1234 12345 ....这样的序列 问第n位数字是几   是数字! 1-9!

  思路:递推关系 主要是位数的计算   用a[i]=a[i-1]+(int)log10((double)i)+1;   每加一个n位数  加log10(n)+1位

      还有取数字    (i-1)/(int)pow((double)10,len-pos)%10   len-pos 是到最后有多少位   数字/10^(len-pos) 就把后面几位截断了再%10就能取出要的数字了

  参考:https://blog.csdn.net/lyy289065406/article/details/6648504

  

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<cmath>
 6 using namespace std;
 7 const int maxn=31269+100;
 8 typedef long long ll;
 9 ll p[maxn];
10 ll sum[maxn];
11 void init(){
12     p[1]=1;
13     sum[1]=1;
14     for(int i=2;i<maxn;i++){
15         p[i]=p[i-1]+1+(int)log10(double(i));
16         sum[i]=sum[i-1]+p[i];
17     }
18 }
19 int find(ll n){
20     int temp=lower_bound(sum+1,sum+maxn,n)-sum;
21     int pos=n-sum[temp-1];
22     int len=0;
23     int i;
24     for( i=1;len<pos;i++){
25         len+=1+int(log10(i*1.0));
26     }
27     return (i-1)/(int)pow(10.0,len-pos)%10;
28 }
29 int main(){
30     ll n;
31     init();
32     int t;
33     scanf("%d",&t);
34     while(t--){
35         scanf("%lld",&n);
36         printf("%d\n",find(n));
37     }
38     return 0;
39 }

猜你喜欢

转载自www.cnblogs.com/ttttttttrx/p/10264948.html
今日推荐