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. 洛谷 P1892 [BOI2003]团伙

    题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...

  2. HDU 1690 最短路

    #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> usi ...

  3. HDU 1724 自适应辛普森法

    //很裸的积分题,直接上模板 #include<stdio.h> #include<math.h> int aa, bb; //函数 double F(double x){ - ...

  4. Codeforces Round #283 (Div. 2) A. Minimum Difficulty【一个数组定义困难值是两个相邻元素之间差的最大值。 给一个数组,可以去掉任意一个元素,问剩余数列的困难值的最小值是多少】

    A. Minimum Difficulty time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. vue 数组遍历方法forEach和map的原理解析和实际应用

    一.前言 forEach和map是数组的两个方法,作用都是遍历数组.在vue项目的处理数据中经常会用到,这里介绍一下两者的区别和具体用法示例. 二.代码 1. 相同点 都是数组的方法 都用来遍历数组 ...

  6. [PHPCMS V9二次开发]自定义字段模型-添加字段类型

    步骤/方法 打开phpcms\modules\content\fields目录,复制文件夹downfiles,并改名为textgroups. 打开phpcms\modules\content\fiel ...

  7. mysql更改密码

    mysql command line client输入密码以后闪退问题的解决: 网上搜到的解决办法(my.ini文件之类的修改对我都没有起到作用).. 所以觉得是自己密码的问题,因为许久不用这个软件了 ...

  8. ie6中兼容性问题总结

    针对firefox ie6 ie7 ie8的css样式中的line-height属性,以前我们大部分都是用!important来hack,对于ie6和firefox测试可以正常显示,但是ie7以上对! ...

  9. LintCode刷题笔记--Flip Bits

    Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n  ...

  10. No.3 Verilog 语言要素

    - 标识符 任意字母.数字."$"和"_"组成,标识符第一个不能是数字. - 注释 ()/*可扩展多行*/ ()//本行结束 - 系统函数 以$字符开始的标识符 ...