日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程 小数位数_使用动态编程的n位数的非递减总数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

編程 小數(shù)位數(shù)

Problem statement:

問題陳述:

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

給定位數(shù)n ,找到具有n位數(shù)字的非遞減總數(shù)。

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.

如果每個(gè)數(shù)字(第一個(gè)數(shù)字除外)都大于或等于前一個(gè)數(shù)字,則數(shù)字不減。 例如,22,223、45567、899是不減數(shù)字,而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.

輸入的第一行包含一個(gè)整數(shù)T,表示測試用例的數(shù)量。 然后是T測試用例。 每個(gè)測試用例的第一行包含整數(shù)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.

在新行中為每個(gè)測試用例用n位數(shù)字打印非降序總數(shù)。 您可能需要使用unsigned long long int,因?yàn)橛?jì)數(shù)可能非常大。

Constraints:

限制條件:

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

Example:

例:

Input: No of test cases, 3n=1 n=2 n=3Output: For n=1 Total count is: 10For n=2 Total count is: 55For n=3 Total count is: 220

Explanation:

說明:

For n=1, The non-decreasing numbers are basically 0 to 9, counting up to 10For, 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.

解決方案可以是遞歸的。 在遞歸中,我們的策略是構(gòu)建字符串(讀取:number),以便在每次遞歸調(diào)用時(shí),要附加的數(shù)字必然大于(或等于)最后一個(gè)數(shù)字。

Say, at any recursion call,

說,在任何遞歸調(diào)用中,

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.

已經(jīng)構(gòu)造的數(shù)字是x 1 x 2 ... x i ,其中i <n ,因此在遞歸調(diào)用中,我們只允許附加等于x i的數(shù)字

So, let's formulate the recursion

因此,讓我們制定遞歸

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

說,遞歸函數(shù)是computerecur(int index,int last,int n)

Where,

哪里,

  • index = current position to append digit

    索引 =當(dāng)前位置追加數(shù)字

  • Last = the previous digit

    最后 =前一位

  • n = number of total digits

    n =總位數(shù)

unsigned long long int computerecur (int index,int last,int n){// base caseif(index has reached the last digit)return 1;unsigned long long int sum=0;for digit to append at current index,i=0 to 9if(i>=last)// recur if I can be appendedsum + =computerecur(index+1,i,n); end forreturn 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.

因此,遞歸的基本思想是,如果要追加的有效數(shù)字(取決于最后一位),則將其追加并針對剩余的數(shù)字進(jìn)行遞歸。

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).

現(xiàn)在,應(yīng)該通過已經(jīng)附加了第一個(gè)數(shù)字來完成對main()函數(shù)的調(diào)用(基本上,我們將把該第一個(gè)數(shù)字保留為遞歸調(diào)用的最后一個(gè)數(shù)字位置)。

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 recursionresult+=computerecur(0,i,n); end for

The result is the ultimate result.

結(jié)果就是最終結(jié)果。

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.

現(xiàn)在,很容易推斷出它會(huì)導(dǎo)致許多子問題重疊,因此我們需要?jiǎng)討B(tài)編程來存儲(chǔ)子問題重疊的結(jié)果。 這就是為什么我使用記憶技術(shù)來存儲(chǔ)已經(jīng)計(jì)算出的子問題結(jié)果的原因。 請參閱以下實(shí)現(xiàn)以了解記憶部分。

C++ Implementation:

C ++實(shí)現(xiàn):

#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 computedif (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

編程 小數(shù)位數(shù)

總結(jié)

以上是生活随笔為你收集整理的编程 小数位数_使用动态编程的n位数的非递减总数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。