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.

【题意】给出p,c1,c2,c3,c4,p是总金额,c1,c2,c3,c4分别是给出的四种面值的硬币的数量,求合起来是p使用最多硬币的情况下,各种硬币都用了多少。

【思路】背包。并且要记录各种硬币的数量,用一个数组path[p],path[p]=p-v[i];p-path[p]=v[i];巧用path解决了各种硬币数量的问题。接着背包问题,他是限定数量的,要判断num[j-v[i]]是否小于c[i],还要判定dp[j]是否存在即是否大于0,以及dp[j-v[i]]+1是否大于dp[j].

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int p;
int v[]= {,,,,};
int c[];
int dp[N],num[N],path[N],ans[N];
int main()
{
while(~scanf("%d%d%d%d%d",&p,&c[],&c[],&c[],&c[]))
{
if(!p&&!c[]&&!c[]&&!c[]&&!c[]) break;
memset(dp,,sizeof(dp));
memset(ans,,sizeof(ans));
memset(path,,sizeof(path));
dp[]=;
for(int i=; i<=; i++)//背包
{
memset(num,,sizeof(num));//每个i更新,都要初始化
for(int j=v[i]; j<=p; j++)
{
if(num[j-v[i]]<c[i]&&dp[j-v[i]]&&dp[j-v[i]]+>dp[j])
{
dp[j]=dp[j-v[i]]+;
num[j]=num[j-v[i]]+;
path[j]=j-v[i];
} }
}
int i=p;
if(dp[p]>)//判断是否存在
{
while(i!=)
{
ans[i-path[i]]++;//巧用path,进行路径记录
i=path[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[],ans[],ans[],ans[]);
}
else printf("Charlie cannot buy coffee.\n");
}
return ;
}

Charlie's Change_完全背包&&路径记录的更多相关文章

  1. 浙大PAT CCCC L3-001 凑零钱 ( 0/1背包 && 路径记录 )

    题目链接 分析 : 就是一个 0/1 背包,但是需要记录具体状态的转移情况 这个可以想象成一个状态转移图,然后实际就是记录路径 将状态看成点然后转移看成边,最后输出字典序最小的路径 这里有一个很巧妙的 ...

  2. UVA 624 CD【01背包+路径记录】

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

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

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

  4. PAT L3-001 凑零钱(01背包dp记录路径)

    韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...

  5. poj--3984--迷宫问题(bfs+路径记录)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  6. ros机器人之小乌龟仿真-路径记录

    ------------恢复内容开始------------ 通过自己不断地摸索,对ros系统有了一定的了解,首先装系统,这一过程中也遇到了很多问题,但通过不断地尝试,经过一天一夜的倒腾,总算是把系统 ...

  7. Android中的路径记录

    Android中的路径记录 | RobinBlog 导航 导航 博客 分类 标签 友链 关于 搜索 Environment.getDataDirectory().getPath()=/dataEnvi ...

  8. 剑指 Offer 34. 二叉树中和为某一值的路径 + 记录所有路径

    剑指 Offer 34. 二叉树中和为某一值的路径 Offer_34 题目详情 题解分析 本题是二叉树相关的题目,但是又和路径记录相关. 在记录路径时,可以使用一个栈来存储一条符合的路径,在回溯时将进 ...

  9. dp之完全背包poj1787(完全背包以及路径记录 推荐)

    题意:有四种硬币,1分,5分,10分,25分,分别有a,b,c,d种,给出一个n分钱,要求你求出组成n分钱最多需要的硬币数量,并且输出组成它的各种硬币的数量...... 学到的东西:这个题目我是用两种 ...

随机推荐

  1. think in java 读书笔记 2 —— 套接字

    目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 套接字基本知识 2 ...

  2. oracle序列为什么不是从1开始

    问题原因: ·当我们使用序列作为插入数据时,如果使用了“延迟段”技术,则跳过序列的第一个值 ·Oracle从 11.2.0.1版本开始,提供了一个“延迟段创建”特性: 即 当我们创建了新的表(tabl ...

  3. linux tar 增量备份命令

    tar --newer-mtime "2013-09-17 00:00:00"   -zcvf /var/www/good.tar.gz    spider/

  4. 闲聊CSS之关于clearfix--清除浮动[转]

    .clearfix:after { content: " "; display: block; clear: both; height:; } .clearfix { zoom:; ...

  5. cf卡中,wtmp文件较大,导致磁盘空间满了

    看了一下,有一个wtmp 和wtmp.1的文件非常大.wtmp记录的是机器注销.启动的信息.由此可见,机器长时间的不断重启,造成该日志记录超级大,把cf的空间给占满了. wtmp日志可以用who和la ...

  6. Ubuntu用户相关基本命令

    Linux是一个用户权限管理得很严格的系统,Ubuntu作为最受欢迎的桌面发行版,提供了简单易用的图形界面工具来管理用户,但是命令行工具往往更强大,用得熟练的话效率会更高.用户管理命令常用的有如下几个 ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. iOS app调试的黑魔法--第三方库

    http://www.cocoachina.com/ios/20140928/9785.html

  9. 【STL】-pair的用法

    初始化: std::pair<int, float> p; //initialize p.first and p.second with zero std::pair<int, co ...

  10. URAL 1519 基础插头DP

    题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...