在以下情况下,将十进制数字称为单调:
。
编写一个程序,该程序在输入上取正数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
说明:让我们从单调数字的示例开始:
所有这些数字都是单调的,因为较高位置的每个数字都是比之前的那个
长度为1且数字为1或2的单调数字是什么?一开始就要问自己一个问题。我们可以看到可能的数字是:
这很容易,现在让我们将问题扩展到数字1、2和3:
现在不同的问题是,仅由1和长度3组成的不同单调数是什么?
现在让我们尝试在二维数组中针对长度为3的数字绘制此非常简单的观察结果,其中第一列是字符串的长度,第一行是可能的数字:
让我们尝试填充第三行第三列(由数字1或2组成的长度为2的单调数字的数量)。应该是:
如果我们仔细观察,我们已经有了该集合的子集,即: –长度为2且由1或2组成的单调数字
–长度为2且由数字2组成的单调数字
我们只需要添加以前的值即可获得更长的值。
最终矩阵应如下所示:
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