编程 小数位数_使用动态编程的n位数的非递减总数

编程 小数位数

Problem statement:

问题陈述:

Given the number of digits n, find the count of total non-decreasing numbers with n digits.

给定位数n ,找到具有n位数字的非递减总数。

A number is non-decreasing if every digit (except the first one) is greater than or equal to the previous digit. For example, 22,223, 45567, 899, are non-decreasing numbers whereas 321 or 322 are not.

如果每个数字(第一个数字除外)都大于或等于前一个数字,则数字不减。 例如,22,223、45567、899是不减数字,而321或322不是。

Input:

输入:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains the integer n.

输入的第一行包含一个整数T,表示测试用例的数量。 然后是T测试用例。 每个测试用例的第一行包含整数n

Output:

输出:

Print the count of total non-decreasing numbers with n digits for each test case in a new line. You may need to use unsigned long long int as the count can be very large.

在新行中为每个测试用例用n位数字打印非降序总数。 您可能需要使用unsigned long long int,因为计数可能非常大。

Constraints:

限制条件:

1 <= T <= 100
1 <= n <= 200

Example:

例:

Input:
No of test cases, 3

n=1
n=2
n=3

Output:
For n=1
Total count is: 10

For n=2
Total count is: 55

For n=3
Total count is: 220

Explanation:

说明:

For n=1,
The non-decreasing numbers are basically 0 to 9, 
counting up to 10

For, n=2,
The non-decreasing numbers can be
00
01
02
..
11
12
13
14
15
.. so on total 55

Solution Approach:

解决方法:

The solution can be recursive. In recursion our strategy will be to build the string (read: number) such that at each recursive call the number to be appended would be necessarily bigger (or equal) than(to) the last one.

解决方案可以是递归的。 在递归中,我们的策略是构建字符串(读取:number),以便在每次递归调用时,要附加的数字必然大于(或等于)最后一个数字。

Say, at any recursion call,

说,在任何递归调用中,

The number already constructed is x1x2...xi where i<n, So at the recursive call we are allowed to append digits only which are equal to greater to xi.

已经构造的数字是x 1 x 2 ... x i ,其中i <n ,因此在递归调用中,我们只允许附加等于x i的数字

So, let's formulate the recursion

因此,让我们制定递归

Say, the recursive function is computerecur(int index, int last, int n)

说,递归函数是computerecur(int index,int last,int n)

Where,

哪里,

  • index = current position to append digit

    索引 =当前位置追加数字

  • Last = the previous digit

    最后 =前一位

  • n = number of total digits

    n =总位数

unsigned long long int computerecur (int index,int last,int n){
    // base case
    if(index has reached the last digit)
        return 1;
    
    unsigned long long int sum=0;
    for digit to append at current index,i=0 to 9
        if(i>=last)
            // recur if I can be appended
            sum + =computerecur(index+1,i,n); 
    end for
    return sum    
End function

So the basic idea to the recursion is that if it's a valid digit to append (depending on the last digit) then append it and recur for the remaining digits.

因此,递归的基本思想是,如果要追加的有效数字(取决于最后一位),则将其追加并针对剩余的数字进行递归。

Now the call from the main() function should be done by appending the first digit already (basically we will keep that first digit as last digit position for the recursion call).

现在,应该通过已经附加了第一个数字来完成对main()函数的调用(基本上,我们将把该第一个数字保留为递归调用的最后一个数字位置)。

So at main,

所以总的来说

We will call as,

我们称之为

unsigned long long int result=0;
    for i=0 to 9 //starting digits
        // index=0, starting digit assigned as 
        // last digit for recursion
        result+=computerecur(0,i,n); 
end for

The result is the ultimate result.

结果就是最终结果。

I would suggest you draw the recursion tree to have a better understanding, Take n=3 and do yourself.

我建议您绘制递归树以更好地理解,取n = 3并自己做。

For, n=2, I will brief the tree below

对于n = 2 ,我将简要介绍下面的树

For starting digit 0
Computerecur(0,0,2) 

Index!=n-1
So
Goes to the loop
And then 
It calls to
Computerecur(1,0,2) // it's for number 00
Computerecur(1,1,2) // it's for number 01
Computerecur(1,2,2) // it's for number 02
Computerecur(1,3,2) // it's for number 03
Computerecur(1,4,2) // it's for number 04
Computerecur(1,5,2) // it's for number 05
Computerecur(1,6,2) // it's for number 06
Computerecur(1,7,2) // it's for number 07
Computerecur(1,8,2) // it's for number 08
Computerecur(1,9,2) // it's for number 09
So on
...

Now, it's pretty easy to infer that it leads to many overlapping sub-problem and hence we need dynamic programming to store the results of overlapping sub-problems. That's why I have used the memoization technique to store the already computed sub-problem results. See the below implementation to understand the memorization part.

现在,很容易推断出它会导致许多子问题重叠,因此我们需要动态编程来存储子问题重叠的结果。 这就是为什么我使用记忆技术来存储已经计算出的子问题结果的原因。 请参阅以下实现以了解记忆部分。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

unsigned long long int dp[501][10];

unsigned long long int my(int index, int last, int n)
{

    if (index == n - 1)
        return 1;

    // memorization, don't compute again what is already computed
    if (dp[index][last] != -1) 
        return dp[index][last];

    unsigned long long int sum = 0;
    for (int i = 0; i <= 9; i++) {
        if (i >= last)
            sum += my(index + 1, i, n);
    }

    dp[index][last] = sum;
    return dp[index][last];
}

unsigned long long int compute(int n)
{
    unsigned long long int sum = 0;
    for (int i = 0; i <= 9; i++) {
        sum += my(0, i, n);
    }
    return sum;
}
int main()
{

    int t, n, item;
    cout << "enter number of testcase\n";
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        cout << "Enter the number of digits,n:\n";
        scanf("%d", &n);
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= 9; j++) {
                dp[i][j] = -1;
            }
        }

        cout << "number of non-decreasing number with " << n;
        cout << " digits are: " << compute(n) << endl;
    }

    return 0;
}

Output:

输出:

enter number of testcase
3
Enter the number of digits,n:
2
number of non-decreasing number with 2 digits are: 55
Enter the number of digits,n:
3
number of non-decreasing number with 3 digits are: 220
Enter the number of digits,n:
6
number of non-decreasing number with 6 digits are: 5005


翻译自: https://www.includehelp.com/icp/total-number-of-non-decreasing-numbers-with-n-digits-using-dynamic-programming.aspx

编程 小数位数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值