zoukankan      html  css  js  c++  java
  • poj 1019 Number Sequence 数学

    题意:
    给你这一串数字
    11212312341234512345612345671234567812345678912345678910123456789101112345678910……
    要我们求出第n个数是多少(从左到右看),例如第2个是1,第三个是2,第八个是2;
    分析:
    log10(x)+1求的是一个数的位数。然后可以这样求出第i个数的位数,a[i]统计到第i个数一共有几个数,即从1到i的每一段有几位数。s[i]统计到i这个数总共的位数。然后对于查找第x位,先在s数组中找到第几段(假设是k),找到在这一段中的位置pos,然后就计算这个位置是哪个数就行了。
    查找的时候可以用二分,不过遍历就足够了。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    const int N=31270;
    ll a[N],s[N];
    void init()
    {
        a[1]=s[1]=1;
        for(int i=2;i<N;i++){
            a[i]=a[i-1]+(ll)log10((double)i)+1;
            s[i]=s[i-1]+a[i];
        }
    }
    int main()
    {
        //freopen("f.txt","r",stdin);
        init();
        int T;scanf("%d",&T);
        while(T--){
            ll n;
            scanf("%lld",&n);
            int t=1;
            while(s[t]<n)t++;
            int pos=n-s[t-1];
            int i,x=0;
            for(i=1;x<pos;i++){
                x+=(int)log10((double)i)+1;
            }
            printf("%d
    ",(i-1)/(int)pow(10.0,x-pos)%10);
        }
        return 0;
    }
    
  • 相关阅读:
    docker-compose
    Cassandra
    npm常用命令
    k8s linux win10
    wsl2 docker 迁移
    docker http 代理
    mysql查看当前所有的数据库和索引大小
    mybatis 遍历list拼接 or查询
    es head crud
    nginx 代理转发mysql
  • 原文地址:https://www.cnblogs.com/01world/p/5762822.html
Copyright © 2011-2022 走看看