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. log4j配置文件的详解

    1.配置根Logger,其语法为: log4j.rootLogger = [ level ] , appenderName, appenderName, … 其中,level 是日志记录的优先级,分为 ...

  2. soapUI参数

    点击File->New Rest Project,填入要测试的URI,确定进入编辑界面: 调整请求方式,添加请求参数,设置参数风格,这里要说一下:style有五种,QUERY是默认常用:TEMP ...

  3. eclipse 中提示tomcat 的端口被占用了 后的最快捷解决方法

    很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就重新配置tomcat  或者去修改tomcat的端口号 ,其实这么做太麻 ...

  4. MATLAB实现矩阵分块相乘

    要实现一下功能,这里$\bf{x}_i$为行向量 $${\bf{A}} = \left[ \begin{array}{l}{{\bf{x}}_1}\\{{\bf{x}}_2}\end{array} \ ...

  5. 5月4日课堂内容:for循环的穷举、迭代

    一.for循环拥有两类: 1.穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 2.迭代: 从初始情况按照规律不断求解中间情况,最终推导出结果. 二.穷举练习 1.单位给发了一张 ...

  6. 源码编译Chrome

    官网描述 http://www.chromium.org/developers/how-tos/build-instructions-windows 为啥还要写这篇博客 太久没在这里写博客 Chrom ...

  7. formvalidator4.1.3 使用过程中一些问题的解决

    在使用formvalidator4.1.3 插件时  发现 正常情况调用时没有问题的,但是我们的项目中需要把css  .js 之类的静态资源用单独的域名加载. 那么问题就来了在 该插件中 有一段这样的 ...

  8. tds 安装找不到已安装的DB2

    应该是没有安装ksh的问题,yum install ksh

  9. memached 服务器lru算法

    1.LRU是Least Recently Used的缩写,即最近最少使用页面置换算法,是为虚拟页式存储管理服务的.LRU算法的提出,是基于这样一个事实:在前面几条指令中使用频繁的页面很可能在后面的几条 ...

  10. 使用ContentProvider管理联系人------添加联系人

    add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 获取程序界面中的三个 ...