📜  计算单调的n位数字

📅  最后修改于: 2021-04-24 05:25:45             🧑  作者: 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}

让我们尝试填充第三行第三列(由数字1或2组成的长度为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