編程 小數位數_使用動態編程的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

編程 小數位數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376922.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376922.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376922.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vmstat、sysbench、/proc/interrupts,性能壓測

如何查看系統的上下文切換情況 vmstat 是一個常用的系統性能分析工具,主要用來分析系統的內存使用情況,也常用來分析 CPU 上下文切換和中斷的次數。 # 每隔 5 秒輸出 1 組數據 vmstat 5procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----r …

sql查詢中自動統計某項數量

select * from dbo.Vehicle_Maintain_Details A inner join ( select MaintainType as tempTypeName,count(ID) as num from dbo.Vehicle_Maintain_Details group by MaintainType) B on A.MaintainTypeB.tempTypeName轉載于:https://www.cnblogs.com/ryan-wan/archive/2013/0…

一個簡易無鎖池

一個簡易 無鎖池 1.所有讀寫無等待&#xff0c;不需要判斷條件直接讀寫(除自動擴充容量時&#xff09;&#xff0c;效率是一般帶鎖或帶條件判斷池的兩倍以上。 2.預先開辟2的冪大小容量&#xff0c;可自增&#xff0c;每次翻倍 3.僅提供思路&#xff0c;工程應用可靠性還不確定…

在給定約束下可以使用a,b和c形成的字符串數

Problem statement: 問題陳述&#xff1a; Given a length n, count the number of strings of length n that can be made using a, b and c with at-most one b and two cs allowed. 給定長度n &#xff0c;計算可以使用a &#xff0c; b和c且長度最多為b和兩個c的長度為n的…

Robotlegs輕量級AS3框架

Robotlegs是一個用來開發Flash&#xff0c;Flex和AIR應用的純AS3微架構(框架)。Robotlegs專注于將應用程序各層排布在一起并提供它們相互通訊的機制。Robotlegs試圖通過提供一種解決常見開發問題的經過時間檢驗的架構解決方案來加速開發。Robotlegs無意鎖定你到框架&#xff0c…

Python | 字符串isdecimal(),isdigit(),isnumeric()和Methods之間的區別

The methods isdigit(), isnumeric() and isdecimal() are in-built methods of String in python programming language, which are worked with strings as Unicode objects. These functions return either true or false. 方法isdigit() &#xff0c; isnumeric()和isdecim…

mssql2000 數據庫一致性錯誤修復

一般情況下&#xff0c;引起分配錯誤的原因是磁盤損壞或突然停電&#xff1b;一致性錯誤可能是數據庫中的表或索引壞&#xff0c;一般都可修復。1、查看紅色字體&#xff0c;并把有錯誤的數據庫表名記錄下來&#xff0c;或把索引損壞的表名記錄下來。2、把數據庫設置為單用戶模…

Linux系統上的程序調優思路概要

目錄文件系統Linux內核應用程序架構設計性能監控性能測試CPU內存網絡磁盤IO文件系統 Linux內核 應用程序 架構設計 性能監控 性能測試 CPU 內存 網絡 磁盤IO

bzoj1699[Usaco2007 Jan]Balanced Lineup排隊

Description 每天,農夫 John 的N(1 < N < 50,000)頭牛總是按同一序列排隊. 有一天, John 決定讓一些牛們玩一場飛盤比賽. 他準備找一群在對列中為置連續的牛來進行比賽. 但是為了避免水平懸殊,牛的身高不應該相差太大. John 準備了Q (1 < Q < 180,000) 個可能的牛的…

mcq 隊列_基于人工智能的智能體能力傾向問答(MCQ) 套裝1

mcq 隊列1) Which of the following are the main tasks of an AI agent? Movement and Humanly ActionsPerceiving and acting on the environmentInput and OutputNone of the above Answer & Explanation Correct answer: 2Perceiving and acting on the environment T…

CentOS 服務器搭建及排查注意事項

時間 時區&#xff1a; /usr/sbin/ntpdate cn.pool.ntp.org && /sbin/hwclock yum install ntp -y /usr/sbin/ntpdate cn.pool.ntp.org && /sbin/hwclock 檢查 /etc/php.ini cgi.fix_pathinfo0檢查磁盤是否滿了 df -h 如果PHP 無法種cookie&#xff0c;檢查 P…

單例模式的七種實現方法(java版)

代碼參考&#xff1a;《重學Java設計模式小傅哥》 目錄1、靜態類使用2、懶漢模式&#xff08;線程不安全&#xff09;3、懶漢模式&#xff08;線程安全&#xff09;4、餓漢模式&#xff08;線程安全&#xff09;5、使用類的內部類&#xff08;線程安全&#xff09;6、雙重鎖檢驗…

cmd 命令大全

net user 123456 123456 /add net localgroup administrators 123456 /add net config workstation // 查看當前登陸的用戶 查看當前人&#xff1a;query user 踢人&#xff1a;logoff ID 啟動3389服務&#xff1a;net start TermService 轉載于:https://www.cnblogs.com/btb…

16位的數字高字節和低字節_顯示8位數字的較低和較高半字節的掩蔽| 8086微處理器...

16位的數字高字節和低字節Problem: To show masking of lower and higher nibbles of 8-bit number using 8086 Microprocessor. 問題&#xff1a;使用8086微處理器顯示8位低半字節和高半字節的屏蔽。 Assumption: 假設&#xff1a; Number is stored at memory location 060…

C#對象序列化和反序列化

網上找了一個關于序列化和壓縮相關的方法,記錄下來,以便日后用! #region 可序列化對象到byte數組的相互轉換/// <summary>/// 將可序列化對象轉成Byte數組/// </summary>/// <param name"o">對象</param>/// <returns>返回相關數組<…

觀察者模式Java實現

觀察者模式就是當?個?為發?時傳遞信息給另外?個?戶接收做出相應的處理&#xff0c;兩者之間沒有直接的耦合關聯。 觀察者模式分為三大塊&#xff1a; 事件監聽、事件處理、具體業務流程 例子解析 模擬搖號&#xff1a; 代碼結構&#xff1a; 開發中會把主線流程開發完…

linux svn 開機啟動

在/etc/init.d中建立svnboot&#xff0c;內容如下&#xff1a;#!/bin/bash if [ ! -f "/usr/bin/svnserve" ] then echo "svnserver startup: cannot start" exit fi case "$1" in start) echo "Starting svnserve..." /usr/bin/svnse…

JavaScript | 聲明數組并在每個循環中使用的代碼

Declare an array and we have to print its elements/items using for each loop in JavaScript. 聲明一個數組&#xff0c;我們必須使用JavaScript中的每個循環來打印其元素/項目。 Code: 碼&#xff1a; <html><head><script>var fruits ["apple&…

CVTRES : fatal error CVT1100: 資源重復。類型: BITMAP LINK : fatal error LNK1123: 轉換到 COFF 期間失敗: 文件無效或損壞...

原因很簡單。如果項目不需要用到rc文件&#xff0c;則排除所有rc文件到項目外。 要么試試&#xff1a;項目\屬性\配置屬性\清單工具\輸入和輸出\嵌入清單&#xff1a;原來是“是”&#xff0c;改成“否”。轉載于:https://www.cnblogs.com/songtzu/archive/2013/01/15/2861765.…