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.
题目大意:
输入p,t1,t2,t3,t4,分别表示需要的钱,1的个数,5的个数,10的个数,25的个数,求凑成价值p的最多硬币的情况。
#include <stdio.h>
#include <string.h>
const int INF=0x3f3f3f3f;
const int N=;
int dp[N],path[N],vis[N];
int v[]={,,,},a[],ans[];
int main()
{
int val;
while(~scanf("%d",&val))
{
int f=val;
for(int i=;i<;i++)
scanf("%d",&a[i]),f+=a[i];
if(!f) break;
for(int i=;i<=val;i++)
dp[i]=-INF;
memset(path,,sizeof path);
path[]=-;
dp[]=;
for(int i=;i<;i++)
{
memset(vis,,sizeof vis);
for(int j=v[i];j<=val;j++)
{
if(dp[j-v[i]]+>dp[j]&&dp[j-v[i]]>=&&vis[j-v[i]]<a[i])
{
dp[j]=dp[j-v[i]]+;
vis[j]=vis[j-v[i]]+;
path[j]=j-v[i];///路径
}
}
}
if(dp[val]<)
printf("Charlie cannot buy coffee.\n");
else
{
memset(ans,,sizeof ans);
int i=val;
while()
{
if(path[i]==-) break;
ans[i-path[i]]++;
i=path[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[v[]],ans[v[]],ans[v[]],ans[v[]]);
}
}
return ;
}

 

Charlie's Change(完全背包记录路径)的更多相关文章

  1. poj1787Charlie's Change(多重背包+记录路径+好题)

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

  2. 01背包记录路径 (例题 L3-001 凑零钱 (30分))

    题意: 就是找出来一个字典序最小的硬币集合,且这个硬币集合里面所有硬币的值的和等于题目中的M 题解: 01背包加一下记录路径,如果1硬币不止一个,那我们也不采用多重背包的方式,把每一个1硬币当成一个独 ...

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

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

  4. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  5. poj 1787 背包+记录路径

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

  6. 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...

  7. UVA 624(01背包记录路径)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA624(01背包记录路径)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. 完全背包记录路径poj1787 好题

    这题有点多重背包的感觉,但还是用完全背包解决,dp[j]表示凑到j元钱时的最大硬币数,pre[j]是前驱,used[j]是凑到j时第i种硬币的用量 △回溯答案时i-pre[i]就是硬币价值 #incl ...

随机推荐

  1. React 实践记录 03 React router

    Introduction 本文主要参考了react router 的官方文档. React Router是一套完整的配合React的路由解决方案,可能你已经知道前端路由,或者知道后端有路由的概念,如下 ...

  2. IT兄弟连 JavaWeb教程 Servlet定义以及环境配置 BS程序和CS程序

    随着网络技术的不断发展,单机的软件程序已难以满足网络计算机的需求.为此,各种各样的网络程序开发体系结构应运而生.其中,运用最多的网络应用程序开发体系结构可以分为两种,一种是基于客户端/服务器的C/S结 ...

  3. CentOS7下Oracle11gR2监听启动错误解决

    oracle监听程序启动失败,错误如下: [oracle@localhost ~]$ lsnrctl start LSNRCTL - Production on -APR- :: Copyright ...

  4. nl

    -b -b -a 表示不论是否为空行,也同样列出行号 -b -t 如果用空行,空行不要列出行号 -n 列出行号表示方法,主要有3中 -n -n ln 行号显示在屏幕的最左方显示 -n rn 行号显示在 ...

  5. iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译

    编号 iOS-Apple苹果官方文档翻译名称 博文链接地址 1 苹果API常用英语名词---iOS-Apple苹果官方文档翻译 http://www.cnblogs.com/ChenYilong/p/ ...

  6. 初习mysql procedure

    1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户 ...

  7. 使用JOSM编辑OpenStreetMap地图

    申明:转载请注明出处! 网上关于JOSM的使用大多只介绍了如何安装和优缺点,对于我这种小白完全还是不会,于是Google了一番,国外关于JOSM的使用的文章还是很多的, 选中一篇讲解的非常详细来翻译, ...

  8. COGS 2334. [HZOI 2016]最小函数值

    时间限制:1 s   内存限制:128 MB [题目描述] 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Aix2+Bix+Ci(x∈N∗).给定这些Ai.Bi和Ci,请求出所有函数的所 ...

  9. 关于自动化测试环境的集成(Jenkins+RobotFramework+TestLink+SVN)

    本人主要从事网络安全产品的测试,由于一些产品功能在后期稳定后每个版本的迭代仍需要投入大量的时间和精力去测试,所以近期计划逐步的去了解自动化测试的一些内容来节省和解放一些资源.由于自己并没有什么编码基础 ...

  10. 历史管理 onhashchange

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...