Charlie's Change

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3176   Accepted: 913

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,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1<=p<=1万,1<=硬币数量<=1万
思路:完全背包+路径记忆。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int v[]= {,,,};
int dp[];
int path[],used[];
int num[];
int ans[];//这个要大于25
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int P;
while(scanf("%d%d%d%d%d",&P,&num[],&num[],&num[],&num[]),(P+num[]+num[]+num[]+num[]))
{
for(int i=; i<=P; i++)dp[i]=-INF;
memset(path,,sizeof(path)); path[]=-;//退出的条件
dp[]=;
for(int i=; i<; i++)
{
memset(used,,sizeof(used));
for(int j=v[i]; j<=P; j++)
{
if(dp[j-v[i]]+>dp[j]&&dp[j-v[i]]>=&&used[j-v[i]]<num[i])
{
//dp[j]存j里用多少枚硬币换。(要多的);used来记录是否超过数量
dp[j]=dp[j-v[i]]+;//压入dp,
used[j]=used[j-v[i]]+;//体积j,里数量加一
path[j]=j-v[i];//记录上一个路径(体积)
}
// printf("dp[%d]=%d \t used[%d]=%d\tpath[%d]=%d\n",j,dp[j],j,used[j],j,path[j]);
}
} if(dp[P]<)
{
printf("Charlie cannot buy coffee.\n");
continue;
} memset(ans,,sizeof(ans));
int i=P;
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 ;
}
/*
12 5 3 1 2
16 0 0 0 1
0 0 0 0 0
*/
不懂看看测试数据;

是不是很清楚了。。哈哈。

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

  1. Charlie's Change_完全背包&&路径记录

    Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...

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

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

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

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

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

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

  5. poj 1787 Charlie's Change (多重背包可作完全背包)

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

  6. Charlie's Change POJ - 1787

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

  7. B - Charlie's Change

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  8. poj1787 Charlie's Change

    Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...

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

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

随机推荐

  1. .net图表之ECharts随笔07-自定义系列(多边形)

    搞了一天多,才勉强搞出了一个不紧凑的六边形统计图,是真的菜. 这里ECharts的用法与06说的同一种,直接使用带all的js 先上个效果图,用面积来表示人数的多少 1. 参数option的toolt ...

  2. ArrayBlockingQueue源码解析(2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 3.3.public void put(E e) throws InterruptedException 原 ...

  3. 暴破助攻提权:ruadmin

    i春秋作家:yangyangwithgnu 1 缘由 千辛万苦拿下的 webshell 不是 www-data 用户就是 networkservice 权限,要想拓展攻击面.扩大战果,提权,是必经之路 ...

  4. VSTO:C#获取文档控件的值

    基础知识准备: VSTO入门 创建Excel解决方案   string[] inputfileNames = { @"C:\1.xls", @"C:\2.xls" ...

  5. 阿里云RDS数据库备份文件恢复到本地数据库

    参考这里:https://help.aliyun.com/knowledge_detail/41817.html 第4.2步要多注释掉一些(应该根据实际报错来注释): [mysqld] innodb_ ...

  6. POJ 2725

    #include <iostream> #include <string> #include <algorithm> #define MAXN 400005 usi ...

  7. centos 7 安装 mysql 5.7

    1.环境 Centos 7 2.下载 官方网站https://dev.mysql.com/downloads/mysql/5.7.html#downloads ,选择要下载的版本,centos 7 同 ...

  8. 【胡思乱想】JNI与线程池的维护

    JNI中,C/C++代码里创建的资源不由Java GC处理,故这里的资源必须由C/C++代码明确释放.在JNI中,C/C++回调Java的方法是调用一个CallXXMethod函数来实现的,如果回调的 ...

  9. Linux 技巧:让进程在后台可靠运行的几种方法(转)

    我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败.如何让命令提交后不受本地关闭终端窗口/网络断开 ...

  10. 代理(Proxy)模式 ,桥梁(Bridge)模式

    一:代理模式 1 根据名字我们就可以理解为:代替别人管理 2 什么情况下使用代理模式呢? 在软件系统中,有些对象有时候由于跨越网络或者其他的障碍,而不能够或者不想直接访问另一个对象,如果直接访问会给系 ...