📜  计算单调的 n 位数字

📅  最后修改于: 2021-09-22 09:52:47             🧑  作者: Mango

如果出现以下情况,则称十进制数为单调数:

   D[\, i]\, \leqslant D[\, i+1]\, 0 \leqslant i \leqslant |D| .

编写一个程序,在输入中接受正数 n 并返回长度为 n 的单调十进制数的个数。数字不能以 0 开头。

例子 :

Input : 1
Output : 9
Numbers are 1, 2, 3, ... 9

Input : 2
Output : 45
Numbers are 11, 12, 13, .... 22, 23
...29, 33, 34, ... 39.
Count is 9 + 8 + 7 ... + 1 = 45

说明:让我们从单调数的例子开始: \{111\}, \{123\}, \{12223333444\}

所有这些数字都是单调的,因为高位的每个数字都是\geq比它之前的那个。
什么是长度为 1 且数字为 1 或 2 的单调数?一开始就问自己是个问题。我们可以看到可能的数字是:
 \{1\}, \{2\}

这很简单,现在让我们将问题扩展到数字 1、2 和 3:
 \{1\}, \{2\}, \{3\}

现在不同的问题,只有1和长度3的不同单调数有哪些?
\{111\}

现在让我们尝试在长度为 3 的二维数组中绘制这个非常简单的观察结果,其中第一列是字符串的长度,第一行是可能的数字:
  \begin{array}{c c c c c c c c c c} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &\\ 1 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &\\ 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &\\ 3 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 &\\ \end{array}

让我们尝试填充第 3 行第 3 列(由长度为 2 的数字 1 或 2 组成的单调数字的数量)。这应该是: \{11\}, \{12\}, \{22\}
如果我们仔细观察,我们已经有了这个集合的子集,即:
\{11\}, \{12\} – 长度为 2 且由 1 或 2 组成的单调数字
\{22\} – 长度为 2 且由数字 2 组成的单调数

我们只需要添加以前的值以获得更长的值。
最终矩阵应如下所示:

  \begin{array}{c c c c c c c c c c} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &\\ 1 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &\\ 2 & 1 & 3 & 6 & 10 & 15 & 21 & 28 & 36 & 47 &\\ 3 & 1 & 4 & 10 & 20 & 35 & 56 & 84 & 120 & 167 &\\ \end{array}

C++
// CPP program to count numbers of n digits
// that are  monotone.
#include 
#include 
  
// Considering all possible digits as
// {1, 2, 3, ..9}
int static const DP_s = 9;
  
int getNumMonotone(int len)
{
  
    // DP[i][j] is going to store monotone
    // numbers of length i+1 considering
    // j+1 digits.
    int DP[len][DP_s];
    memset(DP, 0, sizeof(DP));
  
    // Unit length numbers
    for (int i = 0; i < DP_s; ++i)
        DP[0][i] = i + 1;
  
    // Single digit numbers
    for (int i = 0; i < len; ++i)
        DP[i][0] = 1;
  
    // Filling rest of the entries in bottom
    // up manner.
    for (int i = 1; i < len; ++i)
        for (int j = 1; j < DP_s; ++j)
            DP[i][j] = DP[i - 1][j] + DP[i][j - 1];
  
    return DP[len - 1][DP_s - 1];
}
  
// Driver code.
int main()
{
    std::cout << getNumMonotone(10);
    return 0;
}


Java
// Java program to count numbers 
// of n digits that are monotone.
  
class GFG 
{
    // Considering all possible 
    // digits as {1, 2, 3, ..9}
    static final int DP_s = 9;
      
    static int getNumMonotone(int len)
    {
      
        // DP[i][j] is going to store 
        // monotone numbers of length 
        // i+1 considering j+1 digits.
        int[][] DP = new int[len][DP_s];
      
        // Unit length numbers
        for (int i = 0; i < DP_s; ++i)
            DP[0][i] = i + 1;
      
        // Single digit numbers
        for (int i = 0; i < len; ++i)
            DP[i][0] = 1;
      
        // Filling rest of the entries 
        // in bottom up manner.
        for (int i = 1; i < len; ++i)
            for (int j = 1; j < DP_s; ++j)
                DP[i][j] = DP[i - 1][j] 
                           + DP[i][j - 1];
      
        return DP[len - 1][DP_s - 1];
    }
      
    // Driver code.
    public static void main (String[] args) 
    {
        System.out.println(getNumMonotone(10));
    }
}
  
// This code is contributed by Ansu Kumari.


Python3
# Python3 program to count numbers of n 
# digits that are monotone.
  
# Considering all possible digits as
# {1, 2, 3, ..9}
DP_s = 9
  
def getNumMonotone(ln):
  
    # DP[i][j] is going to store monotone
    # numbers of length i+1 considering
    # j+1 digits.
    DP = [[0]*DP_s for i in range(ln)]
  
    # Unit length numbers
    for i in range(DP_s):
        DP[0][i] = i + 1
  
    # Single digit numbers
    for i in range(ln):
        DP[i][0] = 1
  
    # Filling rest of the entries  
    # in bottom up manner.
    for i in range(1, ln):
  
        for j in range(1, DP_s):
            DP[i][j] = DP[i - 1][j] + DP[i][j - 1]
  
    return DP[ln - 1][DP_s - 1]
  
  
# Driver code
print(getNumMonotone(10))
  
  
# This code is contributed by Ansu Kumari


C#
// C# program to count numbers 
// of n digits that are monotone.
using System;
  
class GFG 
{
    // Considering all possible 
    // digits as {1, 2, 3, ..9}
    static int DP_s = 9;
      
    static int getNumMonotone(int len)
    {
      
        // DP[i][j] is going to store 
        // monotone numbers of length 
        // i+1 considering j+1 digits.
        int[,] DP = new int[len,DP_s];
      
        // Unit length numbers
        for (int i = 0; i < DP_s; ++i)
            DP[0,i] = i + 1;
      
        // Single digit numbers
        for (int i = 0; i < len; ++i)
            DP[i,0] = 1;
      
        // Filling rest of the entries 
        // in bottom up manner.
        for (int i = 1; i < len; ++i)
            for (int j = 1; j < DP_s; ++j)
                DP[i,j] = DP[i - 1,j] 
                        + DP[i,j - 1];
      
        return DP[len - 1,DP_s - 1];
    }
      
    // Driver code.
    public static void Main () 
    {
        Console.WriteLine(getNumMonotone(10));
    }
}
  
// This code is contributed by vt_m.


PHP


输出 :

43758

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程