思路:

完全背包,记录路径。

实现:

 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int v[] = {, , , };
int c[], m;
int dp[], used[], path[];
int main()
{
while (cin >> m >> c[] >> c[] >> c[] >> c[], m || c[] || c[] || c[] || c[])
{
memset(used, , sizeof used);
memset(path, , sizeof path);
path[] = -;
for (int i = ; i <= m; i++) dp[i] = -INF;
dp[] = ;
for (int i = ; i < ; i++)
{
memset(used, , sizeof used);
for (int j = v[i]; j <= m; j++)
{
if (dp[j - v[i]] != -INF && dp[j - v[i]] + > dp[j] && used[j - v[i]] < c[i])
{
dp[j] = dp[j - v[i]] + ;
used[j] = used[j - v[i]] + ;
path[j] = j - v[i];
}
}
}
if (dp[m] <= -INF) { cout << "Charlie cannot buy coffee." << endl; }
else
{
int ans[]; memset(ans, , sizeof ans);
while (path[m] != -)
{
ans[m - path[m]]++;
m = path[m];
}
cout << "Throw in " << ans[v[]] << " cents, " << ans[v[]] << " nickels, " << ans[v[]] << " dimes, and " << ans[v[]] << " quarters." << endl;
}
}
return ;
}

poj1787 Charlie's Change的更多相关文章

  1. poj1787 Charlie's Change

    Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...

  2. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  3. Charlie's Change(完全背包+路径记忆)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 D ...

  4. Charlie's Change(完全背包记录路径)

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  5. Charlie's Change POJ - 1787

    Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...

  6. poj 1787 Charlie's Change (多重背包可作完全背包)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3792   Accepted: 1144 ...

  7. B - Charlie's Change

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  8. [POJ 1787]Charlie's Change (动态规划)

    题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货 ...

  9. POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)

    网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...

随机推荐

  1. FTPUtil工具类

    package com.xxx.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...

  2. Git回退---reset和revert

    今天学习了git回退的两个命令,现在总结一下: 1.git reset 如果想回退错误的提交C和D,只要把指针移到B上 git reset --hard a0fvf8 而这时候,远程仓库的指针还在D上 ...

  3. CF# 368 D2 D

    很容易想到可以它操作序列弄成有向图,果断深搜.但我开始竟然用了一种特醇的方法,每个书架做一次深搜,复杂度O(nq),跑到57个test就不动了.看看代码吧 #include <iostream& ...

  4. iOS自己定义返回button(不影响返回手势)

    此方法能够自己定义返回button,且不影响返回手势. 新方法: self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] in ...

  5. Codeforces Round #306 (Div. 2) A B C

    题目链接:http://codeforces.com/contest/550 A 暴力一发. 代码: #include <iostream> #include <stdio.h> ...

  6. 单点登录之CAS简介

    cas官网http://www.ja-sig.org/products/cas/. ok,如今開始本文的重点内容解说,先来了解一下cas 实现single sign out的原理,如图所看到的:    ...

  7. 协方差矩阵与主成分分析PCA

    今天看论文,作者是用主成分分析(PCA)的方法做的.仔细学习了一下,有一篇博客写的很好,介绍的深入浅出! 协方差:http://pinkyjie.com/2010/08/31/covariance/ ...

  8. cp和scp

    1 两个命令的格式一样 cp src dst scp src dst 将src文件拷贝到dst目的地.cp是本机拷贝,即从本机的一个地方拷贝到另外一个地方. 而scp是拷贝到远程及其还是从远程机器拷贝 ...

  9. gradle使用笔记

    1 gradle user home 默认情况下是-/.gradle目录.可以使用gradle -g [directory]修改. 1.1 ./gradle/caches gradle下载的所有的依赖 ...

  10. Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...