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

Description

Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task.

Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.

Input

Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3, C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.

Output

For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".

Sample Input

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee. 感觉上是多重背包,实际上用完全背包的思路来做很快。 题意:分硬币,有1,5,10,25四种硬币,给定每种硬币的数量,给定要组合成的价值,问刚好达到价值时用的硬币最多的情况。 附上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#define N 10010
#define inf (-0x3f3f3f3f)
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int dp[N],path[N],used[N];
// dp[j] 表示 j 块钱最多由多少块硬币组成,
//path[j] 表示 上一次最多有多少块构成的 j 块钱,used[j] 表示 j 块钱时,已经放了多少同种类的硬币。
int i,j,m,n;
int num[],val[]= {,,,};
while(~scanf("%d %d %d %d %d",&n,&num[],&num[],&num[],&num[]))
{
if(n==&&num[]==&&num[]==&&num[]==&&num[]==)
break;
memset(dp,inf,sizeof(dp));
memset(path,,sizeof(path));
path[]=-;
dp[]=; for(i=; i<; i++)
{
memset(used,,sizeof(used));
for(j=val[i]; j<=n; j++)
{
if(dp[j-val[i]]+>dp[j]&&dp[j-val[i]]>=&&used[j-val[i]]<num[i])
{
dp[j]=dp[j-val[i]]+;
used[j]=used[j-val[i]]+;
path[j]=j-val[i];
}
}
} int ans[];
memset(ans,,sizeof(ans));
if(dp[n]<)
{
printf("Charlie cannot buy coffee.\n");
}
else
{
while(path[n]!=-)
{
ans[n-path[n]]++;
n=path[n];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[val[]], ans[val[]], ans[val[]], ans[val[]]);
}
}
return ;
}

poj 1787 Charlie's Change (多重背包可作完全背包)的更多相关文章

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

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

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

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

  3. poj 1787 Charlie's Change

    // 题意 给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1<=p&l ...

  4. POJ 1787 Charlie&#39;s Change

    多重背包 可行性+路径记录 题意是说你要用很多其它的零钱去买咖啡.最后输出你分别要用的 1,5 ,10 .25 的钱的数量. 多重背包二进制分解.然后记录下 这个状态.最后逆向推就可以. #inclu ...

  5. poj 1787 背包+记录路径

    http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Subm ...

  6. 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)

    *注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...

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

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

  8. 多重背包转化成完全背包 E - Charlie's Change

    http://poj.org/problem?id=1787 这个题目我一看就觉得是一个多重背包,但是呢,我不知道怎么输出路径,所以无可奈何,我就只能看一下题解了. 看了题解发现居然是把多重背包转化成 ...

  9. Charlie's Change POJ - 1787

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

随机推荐

  1. Vue--通过button跳转到其他组件并携带id参数

    一.创建vue文件 ’ <template> <div> goodsCommon<br/> goodsCommon<br/> goodsCommon&l ...

  2. Ajax--同源策略,jsonp跨域传输原理(callback),

    什么是同源策略? 阮一峰的博客 同源策略 同源策略的解决方法: 跨域传输 img 标签的src是可以引入其他域名下的图片 script标签的src属性同理 ,也可以引入其他域名下的js文件,并执行 1 ...

  3. 几道面试题-考察JS的运用

    1.定义一个方法,传入一个string类型的参数,然后将string的每个字符间加个空格返回,比如: spacify('hello world') // => 'h e l l o  w o r ...

  4. RestFul 与 RPC

    原文地址:https://blog.csdn.net/u014590757/article/details/80233901 RPC.REST API深入理解 一:RPC RPC 即远程过程调用(Re ...

  5. Springboot 创建的maven获取resource资源下的文件的两种方式

    Springboot 创建的maven项目 打包后获取resource下的资源文件的两种方式: 资源目录: resources/config/wordFileXml/wordFileRecord.xm ...

  6. SVN经常使用操作

    版权声明:本文为博主原创文章.转载请注明出处. https://blog.csdn.net/Jerome_s/article/details/27950055 Subversion(简称svn)安装 ...

  7. UCloud-201809-001:Redis服务未授权访问漏洞安全预警

    UCloud-201809-001:Redis服务未授权访问漏洞安全预警 尊敬的UCloud用户,您好! 发布时间  2018-09-11更新时间  2018-09-11漏洞等级  HighCVE编号 ...

  8. hdu3879 最大权闭合图

    若a,b 2点能够相连,那么可以得到ci的价值,也就是说a,b是得到c的前提条件,对于每一个点,又有耗费. 对于本题,先求出最多能够得到的利益有多少,最小割=未被 选的用户的收益之和 + 被选择的站点 ...

  9. 两篇论文之CNN中正交操作

    CNN的权值正交性和特征正交性,在一定程度上是和特征表达的差异性存在一定联系的. 下面两篇论文,一篇是在训练中对权值添加正交正则提高训练稳定性,一篇是对特征添加正交性的损失抑制过拟合. 第一篇:Ort ...

  10. 跟我一起安装jenkins

    查看java版本 查看brew版本 升级 java brew cask install java 安装 jenkins brew install jenkins 执行 java -jar /usr/l ...