多重背包 不过用完全背包来做 加个判断当前硬币的数量是否超过限制
还有就是打印路径 觉得打印路径都差不多 UVa打印路径的题目很多 要是字典序要求就更高了
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10010;
int dp[maxn], num[maxn], p[maxn];
int a[10];
int b[10] = {0, 1, 5, 10, 25};
int ans[maxn];
int main()
{
int m;
while(scanf("%d %d %d %d %d", &m, &a[1], &a[2], &a[3], &a[4]) && (m+a[1]+a[2]+a[3]+a[4]))
{
for(int i = 0; i <= m; i++)
dp[i] = -999999999;
dp[0] = 0;
memset(p, 0, sizeof(p));
p[0] = -1;
for(int i = 1; i <= 4; i++)
{
memset(num, 0, sizeof(num));
for(int j = b[i]; j <= m; j++)
{
if(dp[j-b[i]]+1 > dp[j] && dp[j-b[i]] >= 0 && num[j-b[i]] < a[i])
{
dp[j] = dp[j-b[i]]+1;
num[j] = num[j-b[i]]+1;
p[j] = j-b[i];
}
}
}
memset(ans, 0, sizeof(ans));
if(dp[m] < 0)
printf("Charlie cannot buy coffee.\n");
else
{
while(1)
{
if(p[m] == -1)
break;
ans[m-p[m]]++;
m = p[m];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[b[1]],ans[b[2]],ans[b[3]],ans[b[4]]);
}
}
return 0;
}