• POJ_1019 Number Sequence 【递推】


    题目:

    A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another. 
    For example, the first 80 digits of the sequence are as follows: 
    11212312341234512345612345671234567812345678912345678910123456789101112345678910

    Input

    The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)

    Output

    There should be one output line per test case containing the digit located in the position i.

    Sample Input

    2
    8
    3

    Sample Output

    2
    2

    题意分析:

    把这些数,根据构造它们的那个数划分成 1; 1,2; 1,2,3;……

    然后分析一个数字的位数是怎么确定的,其实,因为数是十进制,可以直接取以10为底的对数+1.

    $log_{10}(N)+1$

    在写程序的时候,一定要注意N要用double类型。然后我们可以递推求出所有可能的小于MAXN的数,最后一组数就是31268,这个数可以通过写个测试程序测试一下就出来了。

    然后就可以确定输入的数字在整个序列中的哪个数字组中,接下来就是确定在这个数字组中的那个数中。

    根据序列递增,然后利用求位数的公式,可以直接找到这个数。然后根据N的具体位置,取余取出来即可。

    AC代码:

     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    typedef long long LL;
    const LL MAXN = 2147483647;
    const int Msize = 32000;
    LL Sum[Msize], a[Msize];
    
    void getS()
    {
        Sum[1] = a[1] = 1;
        int i;
        for(i = 2; i < Msize; i++)
        {
            a[i] = a[i-1] + (int)log10((double)i)+1;
            Sum[i] = Sum[i-1] + a[i];
        }
        return;
    }
    
    LL Pow(LL a, LL b)
    {
        LL ans = 1;
        while(b)
        {
            if(b&1)
                ans = ans*a;
            b>>=1;
            a = a*a;
        }
        return ans;
    }
    
    
    int main()
    {
        getS();
        LL N;
        int T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%I64d", &N);
            int i = 1, j, len, pos;
            while(N > Sum[i])
            {
                i++;
            }
            //第N位再i-1所代表的数字区
            pos = N - Sum[i-1];
            len = 0;
            for(j = 1; len < pos ; j++)
            {
                len += (int)log10(j*1.0) + 1;
            }
            //N为在i-1数字区中的第j-1个数中,len为这个数字的最后一位的位置
            int ans = (j-1)/Pow(10, len-pos)%10;
            printf("%d
    ", ans);
    
        }
        return 0;
    }
    

     

      

     

  • 相关阅读:
    Effective C++ 笔记 —— Item 6: Explicitly disallow the use of compiler-generated functions you do not want.
    Oracle DataBase 用户管理与权限管理
    企业邮箱配置SSL发送邮件
    如何知道安装程序在进行安装时对你的电脑到底做了什么?
    架构师笔记:康威定律
    conda 源配置
    myBatis的批量提交方式
    转载:request_time和upstream_response_time详解
    [PowerShell]比较运算符
    [PowerShell]字符串
  • 原文地址:https://www.cnblogs.com/dybala21/p/9816659.html
Copyright © 2020-2023  润新知