📜  n位数字非递减整数

📅  最后修改于: 2021-04-27 06:22:10             🧑  作者: Mango

给定一个整数n> 0(表示位数),该任务是查找本质上不减少的n位正整数的总数。
一个非递减整数是一个从左到右所有数字均为非递减形式的整数。例如:1234、1135等。
注意:前导零也计入非递减整数,例如0000、0001、0023等,也是四位数的非递减整数。

例子 :

Input : n = 1
Output : 10
Numbers are 0, 1, 2, ...9.

Input : n = 2
Output : 55

Input : n = 4
Output : 715

天真的方法:我们生成所有可能的n位数字,然后针对每个数字检查其是否为非递减数。

时间复杂度:(n * 10 ^ n),其中10 ^ n用于生成所有可能的n位数字,n用于检查特定数字是否不变。

动态编程:
如果我们从左到右一位一位地填充数字,则以下条件成立。

  1. 如果当前的最后一位数字是9,我们在剩余的地方只能填9。因此,如果当前最后一位为9,则只能采用一种解决方案。
  2. 如果当前最后一位数字小于9,则可以使用以下公式递归计算计数。
    a[i][j] = a[i-1][j] + a[i][j + 1] 
             For every digit j smaller than 9.
    
    We consider previous length count and count
    to be increased by all greater digits.
    

我们建立一个矩阵a [] [],其中a [i] [j] =所有有效的i位非降序整数(以j或大于j为前导数字)的计数。该解决方案基于以下观察结果。我们按列填充此矩阵,首先计算a [1] [9],然后使用该值计算a [2] [8],依此类推。
在任何时候,如果我们希望计算a [i] [j]表示i位数字不减整数,且前导数字为j或大于j的数字,则我们应将a [i-1] [j]( i-1个数字的整数,应从j或更大的数字开始,因为在这种情况下,如果我们将j放置为其最左边的数字,则我们的数字将是i位数的非递减数字)和a [i] [j + 1](i位数的整数,应以大于等于j + 1的位数开头)。因此, a [i] [j] = a [i-1] [j] + a [i] [j + 1]

C/C++
// C++ program for counting n digit numbers with
// non decreasing digits
#include 
using namespace std;
  
// Returns count of non- decreasing numbers with
// n digits.
int nonDecNums(int n)
{
    /* a[i][j] = count of all possible number
       with i digits having leading digit as j */
    int a[n + 1][10];
  
    // Initialization of all 0-digit number
    for (int i = 0; i <= 9; i++)
        a[0][i] = 1;
  
    /* Initialization of all i-digit
      non-decreasing number leading with 9*/
    for (int i = 1; i <= n; i++)
        a[i][9] = 1;
  
    /* for all digits we should calculate
      number of ways depending upon leading
      digits*/
    for (int i = 1; i <= n; i++)
        for (int j = 8; j >= 0; j--)
            a[i][j] = a[i - 1][j] + a[i][j + 1];
  
    return a[n][0];
}
  
// driver program
int main()
{
    int n = 2;
    cout << "Non-decreasing digits = "
         << nonDecNums(n) << endl;
    return 0;
}


Java
// Java program for counting n digit numbers with
// non decreasing digits
import java.io.*;
  
class GFG {
  
    // Function that returns count of non- decreasing numbers
    // with n digits
    static int nonDecNums(int n)
    {
        // a[i][j] = count of all possible number
        // with i digits having leading digit as j
        int[][] a = new int[n + 1][10];
  
        // Initialization of all 0-digit number
        for (int i = 0; i <= 9; i++)
            a[0][i] = 1;
  
        // Initialization of all i-digit
        // non-decreasing number leading with 9
        for (int i = 1; i <= n; i++)
            a[i][9] = 1;
  
        // for all digits we should calculate
        // number of ways depending upon leading
        // digits
        for (int i = 1; i <= n; i++)
            for (int j = 8; j >= 0; j--)
                a[i][j] = a[i - 1][j] + a[i][j + 1];
  
        return a[n][0];
    }
  
    // driver program
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println("Non-decreasing digits = " + nonDecNums(n));
    }
}
  
// Contributed by Pramod Kumar


Python3
# Python3 program for counting n digit 
# numbers with non decreasing digits 
import numpy as np
  
# Returns count of non- decreasing 
# numbers with n digits. 
def nonDecNums(n) :
          
    # a[i][j] = count of all possible number 
    # with i digits having leading digit as j 
    a = np.zeros((n + 1, 10)) 
  
    # Initialization of all 0-digit number 
    for i in range(10) :
        a[0][i] = 1
  
    # Initialization of all i-digit 
    # non-decreasing number leading with 9
    for i in range(1, n + 1) : 
        a[i][9] = 1
  
    # for all digits we should calculate 
    # number of ways depending upon 
    # leading digits
    for i in range(1, n + 1) :
        for j in range(8, -1, -1) : 
            a[i][j] = a[i - 1][j] + a[i][j + 1]
  
    return int(a[n][0]) 
  
# Driver Code 
if __name__ == "__main__" : 
  
    n = 2
    print("Non-decreasing digits = ", 
                       nonDecNums(n))
  
# This code is contributed by Ryuga


C#
// C# function to find number of diagonals
// in n sided convex polygon
using System;
  
class GFG {
      
    // Function that returns count of non- 
    // decreasing numbers with n digits
    static int nonDecNums(int n)
    {
        // a[i][j] = count of all possible number
        // with i digits having leading digit as j
        int[, ] a = new int[n + 1, 10];
  
        // Initialization of all 0-digit number
        for (int i = 0; i <= 9; i++)
            a[0, i] = 1;
  
        // Initialization of all i-digit
        // non-decreasing number leading with 9
        for (int i = 1; i <= n; i++)
            a[i, 9] = 1;
  
        // for all digits we should calculate
        // number of ways depending upon leading
        // digits
        for (int i = 1; i <= n; i++)
            for (int j = 8; j >= 0; j--)
                a[i, j] = a[i - 1, j] + a[i, j + 1];
  
        return a[n, 0];
    }
  
    // driver program
    public static void Main()
    {
        int n = 2;
        Console.WriteLine("Non-decreasing digits = " + 
                                       nonDecNums(n));
    }
}
  
// This code is contributed by Sam007


PHP
= 0; $j--)
            $a[$i][$j] = $a[$i - 1][$j] + 
                         $a[$i][$j + 1];
  
    return $a[$n][0];
}
  
// Driver Code
$n = 2;
echo "Non-decreasing digits = ",
            nonDecNums($n),"\n";
  
// This code is contributed by m_kit
?>


输出 :

Non-decreasing digits = 55

时间复杂度: O(10 * n)等于O(n)。