题设:
假设我们有8种不同面值的硬币{1,2,5,10,20,50,100},用这些硬币组合够成一个给定的数值n。例如n=200,那么一种可能的组合方式为 200 = 3 * 1 + 1*2 + 1*5 + 2*20 + 1 * 50 + 1 * 100。 问总过有多少种可能的组合方式?
思路分析:
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
(V1,V2,...,Vm)代表硬币值,其中K是该xm能取的最大数值K = sum / Vm。
可以递推出如下公式:
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]
初始值设定:
如果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。
代码如下:
/*
2 * Filename :coins.cpp
3 * Description: solve coin combinations using dynamic programing
4 * Complier: g++
5 * Author: python27
6 */
#include
#include
#include
#include
using namespace std;
/****************************************************************
* coin Combinations: using dynamic programming
*
* Basic idea:
* dp[i][j] = sum(dp[i-1][j-k*coins[i-1]]) for k = 1,2,..., j/coins[i-1]
* dp[0][j] = 1 for j = 0, 1, 2, ..., sum
*
* Input:
* coins[] - array store all values of the coins
* coinKinds - how many kinds of coins there are
* sum - the number you want to construct using coins
*
* Output:
* the number of combinations using coins construct sum
*
* Usage:
* c[3] = {1, 2, 5};
* int result = coinCombinations(c, 3, 10);
*
****************************************************************/
int coinCombinations(int coins[], int coinKinds, int sum)
{
// 2-D array using vector: is equal to: dp[coinKinds+1][sum+1] = {0};
vector > dp(coinKinds + 1);
for (int i = 0; i <= coinKinds; ++i)
{
dp[i].resize(sum + 1); //reset dp[i] to the size of sum+1
}
/*
for (int i = 0; i <= coinKinds; ++i)
{
for (int j = 0; j <= sum; ++j)
{
dp[i][j] = 0;
}
}
*/
//init: dp[i][0] = 1; i = 0, 1, 2 ..., coinKinds
//Notice: dp[0][0] must be 1, althongh it make no sense that
//using 0 kinds of coins construct 0 has one way. but it is the foundation
//of iteration. without it everything based on it goes wrong
for (int i = 0; i <= coinKinds; ++i)
{
dp[i][0] = 1;
}
for(int j = 1;j <= sum; j++)
{
dp[0][j] = 0;
}
// iteration: dp[i][j] = sum(dp[i-1][j - k*coins[i-1]])
// k = 0, 1, 2, ... , j / coins[i-1]
for (int i = 1; i <= coinKinds; ++i)
{
for (int j = 1; j <= sum; ++j)
{
dp[i][j] = 0;
for (int k = 0; k <= j / coins[i-1]; ++k)
{
dp[i][j] += dp[i-1][j - k * coins[i-1]];
}
}
}
return dp[coinKinds][sum];
}
int main()
{
int coins[7] = {1, 2, 5, 10, 20, 50, 100};
int sum = 200;
int result = coinCombinations(coins, 7, 5);
cout << "using 8 kinds of coins construct 200, combinations are: " << endl;
cout << result << endl;
return 0;
}
如果要输出所有的可能的组合结果?