原文地址:https://www.jianshu.com/p/3069df45b36a
问题描述
100元换零钱1元、2元、5元、10元、20元、50元有多少种组合方案?
解题思路
使用动态规划来求解,使用\(F(N,M)\)表示用不超过第\(M\)个面值(从小到大排序)的零钱来表示\(N\)的所有组合方案数,则
\[{\begin{equation}\begin{aligned}
F(N,M) = \left\{\begin{array}{rcl}
F(N,M-1)+F(N-VALUE[M],M) && {N-VALUE[M] \ge 0}\\
F(N,M-1) && {N-VALUE[M] < 0}
\end{array}\right.
\end{aligned}\end{equation}}\]
程序实现
int main()
{
int val[7] = { 0,1,2,5,10,20,50 };
int f[101][7];
memset(f, 0, sizeof(f));
for (int j = 0; j <= 6; j++)
f[0][j] = 1;
for (int j = 1; j <= 6; j++)
{
for (int i = 1; i <= 100; i++)
{
if (i - val[j] < 0)
f[i][j] = f[i][j - 1];
else
f[i][j] = f[i - val[j]][j] + f[i][j - 1];
}
}
cout << f[100][6] << endl;
system("pause");
return 0;
}