1. 程式人生 > >硬幣面值組合問題

硬幣面值組合問題

問題描述

  假設我們有8種不同面值的硬幣{1,2,5,10,20,50,100,200},用這些硬幣組合夠成一個給定的數值n。例如n=200,那麼一種可能的組合方式為 200 = 3 * 1 + 1*2 + 1*5 + 2*20 + 1 * 50 + 1 * 100. 問總過有多少種可能的組合方式? (這道題目來自著名程式設計網站ProjectEuler, 點選這裡檢視原題目) 類似的題目還有:

  [華為面試題] 1分2分5分的硬幣三種,組合成1角,共有多少種組合

  [創新工廠筆試題] 有1分,2分,5分,10分四種硬幣,每種硬幣數量無限,給定n分錢,有多少中組合可以組成n分錢

問題分析

  給定一個數值sum,假設我們有m種不同型別的硬幣{V1, V2, ..., Vm},如果要組合成sum,那麼我們有

sum = x1 * V1 + x2 * V2 + ... + xm * Vm 

求所有可能的組合數,就是求滿足前面等值的係數{x1, x2, ..., xm}的所有可能個數。

  [思路1] 當然我們可以採用暴力列舉,各個係數可能的取值無非是x1 = {0, 1, ..., sum / V1}, x2 = {0, 1, ..., sum/ V2}等等。這對於硬幣種類數較小的題目還是可以應付的,比如華為和創新工廠的題目,但是複雜度也很高O(sum/V1 * sum/V2 * sum/V3 * ...)

  [思路2] 從上面的分析中我們也可以這麼考慮,我們希望用m種硬幣構成sum,根據最後一個硬幣Vm的係數的取值為無非有這麼幾種情況,xm分別取{0, 1, 2, ..., sum/Vm},換句話說,上面分析中的等式和下面的幾個等式的聯合是等價的。

sum = x1 * V1 + x2 * V2 + ... + 0 * Vm

sum = x1 * V1 + x2 * V2 + ... + 1 * Vm

sum = x1 * V1 + x2 * V2 + ... + 2 * Vm

...

sum = x1 * V1 + x2 * V2 + ... + K * Vm  

  其中K是該xm能取的最大數值K = sum / Vm。可是這又有什麼用呢?不要急,我們先進行如下變數的定義:

dp[i][sum] = 用前i種硬幣構成sum 的所有組合數。

  那麼題目的問題實際上就是求dp[m][sum],即用前m種硬幣(所有硬幣)構成sum的所有組合數。在上面的聯合等式中:當xn=0時,有多少種組合呢? 實際上就是前i-1種硬幣組合sum,有dp[i-1][sum]種! xn = 1 時呢,有多少種組合? 實際上是用前i-1種硬幣組合成(sum - Vm)的組合數,有dp[i-1][sum -Vm]種; xn =2呢, dp[i-1][sum - 2 * Vm]種,等等。所有的這些情況加起來就是我們的dp[i][sum]。所以:

dp[i][sum] = dp[i-1][sum - 0*Vm] + dp[i-1][sum - 1*Vm]

+ dp[i-1][sum - 2*Vm] + ... + dp[i-1][sum - K*Vm]; 其中K = sum / Vm

  換一種更抽象的數學描述就是:

遞迴公式

  通過此公式,我們可以看到問題被一步步縮小,那麼初始情況是什麼呢?如果sum=0,那麼無論有前多少種來組合0,只有一種可能,就是各個係數都等於0;

dp[i][0] = 1   // i = 0, 1, 2, ... , m

  如果我們用二位陣列表示dp[i][sum], 我們發現第i行的值全部依賴與i-1行的值,所以我們可以逐行求解該陣列。如果前0種硬幣要組成sum,我們規定為dp[0][sum] = 0. 

程式原始碼

  通過上面的討論,我們最終可以寫出下面的程式碼來求解該類問題:

 1 /* 
 2  * Filename :coins.cpp
 3  * Description: solve coin combinations using dynamic programing
 4  * Complier: g++
 5  * Author: python27
 6  */
 7 #include <iostream>
 8 #include <string>
 9 #include <cmath>
10 #include <vector>
11 
12 using namespace std;
13 
14 /****************************************************************
15  * coin Combinations: using dynamic programming
16  *
17  * Basic idea:
18  * dp[i][j] = sum(dp[i-1][j-k*coins[i-1]]) for k = 1,2,..., j/coins[i-1]
19  * dp[0][j] = 1 for j = 0, 1, 2, ..., sum
20  * 
21  * Input:
22  * coins[] - array store all values of the coins
23  * coinKinds - how many kinds of coins there are
24  * sum - the number you want to construct using coins
25  *
26  * Output:
27  * the number of combinations using coins construct sum
28  *
29  * Usage:
30  * c[3] = {1, 2, 5};
31  * int result = coinCombinations(c, 3, 10);
32  *
33  ****************************************************************/
34 int coinCombinations(int coins[], int coinKinds, int sum)
35 {
36     // 2-D array using vector: is equal to: dp[coinKinds+1][sum+1] = {0};
37     vector<vector<int> > dp(coinKinds + 1);
38     for (int i = 0; i <= coinKinds; ++i)
39     {
40         dp[i].resize(sum + 1);
41     }
42     for (int i = 0; i <= coinKinds; ++i)
43     {
44         for (int j = 0; j <= sum; ++j)
45         {
46             dp[i][j] = 0;
47         }
48     }
49 
50     //init: dp[i][0] = 1; i = 0, 1, 2 ..., coinKinds
51     //Notice: dp[0][0] must be 1, althongh it make no sense that
52     //using 0 kinds of coins construct 0 has one way. but it the foundation
53     //of iteration. without it everything based on it goes wrong
54     for (int i = 0; i <= coinKinds; ++i)
55     {
56         dp[i][0] = 1;
57     }
58 
59     // iteration: dp[i][j] = sum(dp[i-1][j - k*coins[i-1]])
60     // k = 0, 1, 2, ... , j / coins[i-1]
61     for (int i = 1; i <= coinKinds; ++i)
62     {
63         for (int j = 1; j <= sum; ++j)
64         {
65             dp[i][j] = 0;
66             for (int k = 0; k <= j / coins[i-1]; ++k)
67             {
68                 dp[i][j] += dp[i-1][j - k * coins[i-1]];
69             }
70         }
71     }
72 
73     return dp[coinKinds][sum];
74 }
75 
76 int main()
77 {
78     int coins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
79     int sum = 200;
80     int result = coinCombinations(coins, 8, 200);
81     cout << "using 8 kinds of coins construct 200, combinations are: " << endl;
82     cout << result << endl;
83     return 0;
84 }

  聰明的讀者或許已經發現,在演算法的描述中說明用動態規劃的方法來求解此問題,什麼?動態規劃,我們什麼時候用動態規劃了?哈哈,在我們寫出遞迴公式並且給出初始解的時候,我們就已經在用動態規劃了。

  動態規劃的基本思想就是將待求解問題分解為若干子問題,(如本題中我們將dp[i][j]分解為若干dp[i-1][j-x]的問題),先求解這些子問題並將結果儲存起來( 我們用dp[][]二維陣列儲存子結果),若在求解較大的問題時用到較小子問題的結果,可以直接取用(求dp[i][j]時用dp[i-1][x]的結果),從而免去重複計算。動態規劃是一種非常強大的演算法思想,無論做過多少動態規劃的題目,下一次依然會被動態規劃的強大所震撼。隨後的部落格中,我們會更多的接觸動態規劃。你可以在後面的參考文獻中找到更多有用的資源。 

參考文獻

[1] ProjectEuler: http://projecteuler.net/problem=31

[2] Topcoder Algorithm tutorial: http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg

[3] Sanjoy Dasgupta. 演算法概論. 清華大學出版社,2008: 173 - 193.  

[4] Thomas H. Cormen, et al. 演算法導論. 機械工業出版社,2011: 192 - 212.

轉自:http://www.cnblogs.com/python27/archive/2013/09/05/3303721.html