擲骰子

Description:

描述:

In this article, we are going to see a dynamic programing problem which can be featured in any interview rounds.

在本文中,我們將看到一個動態的編程問題,該問題可以在任何采訪回合中體現。

Problem statement:

問題陳述:

Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown.

給定n個骰子,每個骰子有m個面(從1m編號),請找到獲得和X的方法數量。 X是拋出所有骰子時每個面上的值之和。

    Input:
n=3
m=3
X=6
Output:
Total number of ways are: 7

Explanation:

說明:

    Total number of dices: 3 say x1,x2,x3
Number of faces on each dice: 3 (1 to 3)
Total sum to be achieved: 6
We will write as xi(j)which means face value of dice xi is j 
So sum 6 can be achieved in following ways:
6=x1(1)+x2(2)+x3(3)
6=x1(1)+x2(3)+x3(2)
6=x1(2)+x2(2)+x3(2)
6=x1(2)+x2(3)+x3(1)
6=x1(2)+x2(1)+x3(3)
6=x1(3)+x2(2)+x3(3)
6=x1(3)+x2(3)+x3(1)
This are total 7 ways to achieve the sum.

Solution Approach:

解決方法:

If it was only 1 dice, then if X<=m, the answer would be 1 else 0. Since there is only one way to achieve the sum if possible as there is only one dice.

如果只有1個骰子,那么如果X <= m ,答案將是1否則為0。由于只有一種骰子,因此只有一種方法可以實現總和。

Now when n, number of dice>1, then the problem becomes a recursive one

現在,當n ,骰子數> 1時,問題就變成了遞歸問題

We can think of the recursive function as?f(n,X) where n is number of dice and X is desired sum.

我們可以將遞歸函數視為f(n,X) ,其中n是骰子數, X是期望的和。

A single dice has m choices, which means the face can have values ranging 1 to m
So,

一個骰子有m個選擇,這意味著該面Kong的取值范圍為1到m
所以,

Recursively we can write,

我們可以遞歸地寫

dice throw formula

That means summation of all choices for this particular dice to have face value 1 to minimum(X, m)

這意味著該特定骰子的所有選擇的總和的面值為1到最小值(X,m)

For our example case, n=3, m=3, X=6

對于我們的示例情況, n = 3,m = 3,X = 6

So, we need to find f(3,6)

因此,我們需要找到f(3,6)

    f(3,6)=f(2,5)+f(2,4)+f(2,3)

f(2,5), f(2,4), f(2,3) all are sub problems themselves which are needed to be solved further. This would generate a recursion tree.

f(2,5),f(2,4),f(2,3)本身都是子問題,需要進一步解決。 這將生成一個遞歸樹。

Of course, we have base cases for single dice which is f(1,i)=1 for i=1 to m

當然,我們有單個骰子的基本情況, 對于i = 1到m,f(1,i)= 1

But this recursion will generate many overlapping sub problems, hence, we need to convert it to dynamic programing.

但是這種遞歸將產生許多重疊的子問題,因此,我們需要將其轉換為動態編程。

    1)  Declare dp[n+1][x+1] similar to f(n,x). Initialize it to 0.
2)  Implement the base case f(1,i)
for i=1 to i minimum(m ,x)
dp[1][i]=1;
3)  Fill the other values as per recursion relation
for i=2 to n //iterate for number of dices
for j=1 to x //iterate for sums
for k=1 to minimum(m ,j)
//iterate for face values up to minimum(m,j),j be the subsum
dp[i][j]+=dp[i-1][j-k];
end for
end for
end for    
4)  The answer is dp[n][x]

C++ Implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
long long int dicethrow(int m, int n, int x) {
if (m * n < x)
return 0;
long long dp[n + 1][x + 1];
memset(dp, 0, sizeof(dp));
//base case
for (int i = 1; i <= m && i <= x; i++)
dp[1][i] = 1;
for (int i = 2; i <= n; i++) { //iterate for number of dices
for (int j = 1; j <= x; j++) { //iterate for sums
//iterate for face values up to minimum(m,j),j be the subsum
for (int k = 1; k <= m & k < j; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
return dp[n][x];
}
int main() {
int n, m, x;
cout << "Enter number of dices, n:\n";
cin >> n;
cout << "Enter number of faces on a dice, m:\n";
cin >> m;
cout << "Enter sum, X:\n";
cin >> x;
cout << "Number of ways to achieve sum: " << dicethrow(m, n, x) << endl;
return 0;
}

Output

輸出量

RUN 1:
Enter number of dices, n:
3
Enter number of faces on a dice, m:
3
Enter sum, X:
6
Number of ways to achieve sum: 7
RUN 2:
Enter number of dices, n:
3
Enter number of faces on a dice, m:
3
Enter sum, X:
12
Number of ways to achieve sum: 0

In the second output there is no way to acquire the sum which can be verified as m*n<X. It's better practise to keep such base case to optimize your code :)

在第二個輸出中,無法獲取可以驗證為m * n <X的和 。 最好保留此類基本情況以優化代碼:)

翻譯自: https://www.includehelp.com/icp/dice-throw.aspx

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

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

相關文章

《YOLO算法筆記》(草稿)

檢測算法回顧 5、6年前的檢測算法大體如下&#xff1a; 手動涉及特征時應該考慮的因素&#xff1a; 1、尺度不變性 2、光照不變性 3、旋轉不變性 這一步驟稱為特征工程&#xff0c;最重要的一個算法稱為sift&#xff0c;(回顧SIFT講解)體現了上述所有的觀點。 在分類的過程中…

U盤安裝Centos6.3

一 首先下載Centos6.3的光盤鏡像文件&#xff0c;網上到鏡像實在是太多了。 CentOS-6.3-i386-bin-DVD1.iso CentOS-6.3-i386-bin-DVD2.iso 二 下載個新版本的UltraISO, 在其菜單“啟動”下有“寫入硬盤鏡像“功能到&#xff0c;原來用到綠色版本是8.6.2.2011不支持&#xff0c;…

[轉]粵語固有辭彙與漢語北方話辭彙對照

本文轉自&#xff1a;http://beta.wikiversity.org/wiki/%E7%B2%B5%E8%AA%9E%E5%9B%BA%E6%9C%89%E8%BE%AD%E5%BD%99%E8%88%87%E6%BC%A2%E8%AA%9E%E5%8C%97%E6%96%B9%E8%A9%B1%E8%BE%AD%E5%BD%99%E5%B0%8D%E7%85%A7 粵語固有辭彙與漢語北方話辭彙對照 「粵語」&#xff08;或稱「…

openlayer調用geoserver發布的地圖實現地圖的基本功能

轉自&#xff1a;http://starting.iteye.com/blog/1039809 主要實現的功能有放大&#xff0c;縮小&#xff0c;獲取地圖大小&#xff0c;平移&#xff0c;線路測量&#xff0c;面積測量&#xff0c;拉寬功能&#xff0c;顯示標注&#xff0c;移除標注&#xff0c;畫多邊形獲取經…

LLVM與Codegen技術

LLVM 百度百科 LLVM是構架編譯器(compiler)的框架系統&#xff0c;以C編寫而成&#xff0c;用于優化以任意程序語言編寫的程序的編譯時間(compile-time)、鏈接時間(link-time)、運行時間(run-time)以及空閑時間(idle-time)&#xff0c;對開發者保持開放&#xff0c;并兼容已有…

跟烏克蘭人學編程1

今天要Disable一個菜單&#xff0c;工程項目多&#xff0c;不容易找。 烏克蘭人建議我用Spy&#xff0c;將靶拖到目標窗體上就可以看到類名。轉載于:https://www.cnblogs.com/SunWentao/archive/2012/12/19/2825220.html

html網頁轉圖片_HTML圖片

html網頁轉圖片HTML圖片 (HTML Images) Images are visuals of something that look elegant. In web pages, images are used to create a good and appealing design. 圖像是外觀精美的視覺效果。 在網頁中&#xff0c;圖像用于創建良好且吸引人的設計。 The <img> ta…

Android學習拾遺

1. java中的flush()作用&#xff1a;強制將輸出流緩沖區的數據送出。 2. 文件存儲&#xff1a; 存儲到內部&#xff1a;另外使用一個class實現&#xff0c;最開始初始化用了this,后來放在這里不合適&#xff0c;改成了帶參數的構造方法。 包括存儲、讀取、追加 讀取&#xff1a…

OLAP 技術之列式存儲與數據壓縮(快查詢方法之一)

前言 列式存儲和數據壓縮&#xff0c;對于一款高性能數據庫來說是必不可少的特性。一個非常流行的觀點認為&#xff0c;如果你想讓查詢變得更快&#xff0c;最簡單且有效的方法是減少數據掃描范圍和數據傳輸時的大小&#xff0c;而列式存儲和數據壓縮就可以幫助我們實現上述兩…

sql 視圖嵌套視圖_SQL視圖

sql 視圖嵌套視圖SQL | 觀看次數 (SQL | Views) Views in SQL are virtual tables. A view also has rows and columns as theyre during a real table within the database. We will create a view by selecting fields from one or more tables present within the database.…

Postgresql多線程hashjoin(inner join)

pg hashjoin 節點大致步驟&#xff1a; 1、分塊與分桶。對一個表hash時&#xff0c;確定塊數和桶數量。&#xff08;一塊被劃分為10個元組的桶&#xff09;確定分塊號與分桶號是由hashvalue決定的。 2、執行&#xff1a; 1、順序獲取S表中所有元組&#xff0c;對每一條元組Has…

iframe實現局部刷新和回調--開篇

今天做項目遇到一個問題。就是提交表單的時候&#xff0c;驗證用戶名是否存在和驗證碼是否正確。 當驗證碼或者用戶名存在的時候。在后臺彈窗提示。可頁面原本file里面符合要求的值刷新沒了。用戶體驗不好。因為用ifream刷新技術已不是什么新鮮技術。所以網上有大把的資料可參考…

Java文件類boolean setExecutable(boolean exec_file,boolean owner_access)方法,帶示例

文件類boolean setExecutable(boolean exec_file&#xff0c;boolean owner_access) (File Class boolean setExecutable(boolean exec_file , boolean owner_access)) This method is available in package java.io.File.setExecutable(boolean exec_file , boolean owner_acc…

OLTP 系統和 OLAP 系統的核心設計思想

關于 OLTP 系統和 OLAP 系統的核心設計思想 數據存儲系統的關于查詢的典型操作&#xff1a; -- 第一種需求&#xff1a; 根據 key&#xff08;1&#xff09; 找 value&#xff08;name,age&#xff09;&#xff0c; 單點查詢 select name, age from student where id 1; stu…

虛擬機

vt-x 虛擬技術的硬盤支持。想像成“硬解碼”的東東。不是裝虛擬機必須的&#xff0c;但有它效果會好些。 vt-x檢測工具&#xff1a;securable.exe 下載地址&#xff1a;http://pan.baidu.com/s/1kTBOvzD Hardware Virtualization選項&#xff1a; no [CPU和BIOS都不支持VT] loc…

算法(轉)

歡迎自薦和推薦鏈接。 算法 優秀博客推薦&#xff1a;各種數據結構與算法知識入門經典&#xff08;不斷更新)基本算法 貪心算法&#xff1a;貪心算法 作者&#xff1a;獨酌逸醉 貪心算法精講 作者&#xff1a;3522021224 遞歸和分治&#xff1a;遞歸與分治策略 …

sjf調度算法_如何通過靜態方法預測SJF調度中未來過程的突發時間?

sjf調度算法In SJF Scheduling, CPU is assigned to the process having the smallest burst time but it can not be implemented practically, because we dont know burst time of the arrived processes in advance. 在SJF Scheduling中 &#xff0c;將CPU分配給具有最短突…

flask 知識點總結

request對象的常用屬性具體使用方法如下:request.headers, request.headers.get(If-None-Match)request.json, request.json[value] 或 request.json.get(detail_msg, "")request.args, request.args.get(limit, 10)來獲取query parametersrequest.form, request.for…

Postgresql中的hybrid hash join(無狀態機講解)

hybrid hash join hybrid hash join是基于grace hash join 的優化。 在postgresql中的grace hash join 是這樣做的&#xff1a;inner table太大不能一次性全部放到內存中&#xff0c;pg會把inner table 和outer table按照join的key分成多個分區&#xff0c;每個分區(有一個inn…

末日中的黎明

哈哈&#xff0c; 今天是2012-12-21&#xff0c;傳說中的世界末日&#xff0c;不過現在看來&#xff0c;一切都是空的。。。 在這個容易記憶的日子里&#xff0c;我的博客開通了。他將伴隨我以后的學習開發&#xff0c;期望我能充分利用博客&#xff0c;幫我養成常總結、常記筆…