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. 关于Entity Framework的概念及搭建

    什么是EF? ADO.NET Entity Framework 是一个对象-关系的映射架构. 它支持直接定义完全独立于数据库结构的实体类,并把它们映射到数据库的表和关系上. 三种编程模型: 数据库优先 ...

  2. nginx windown命令

    cmd进入nginx程序文件夹启动nginx:start nginx nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx nginx -t ...

  3. cad.net的undo返回操作

    这是提供给许多从lisp转移到c#的开发人员的一个函数,这个函数利用后绑代码实现undo返回操作. 本代码由edata提供: edata博客 /// <summary> /// 命令动作编 ...

  4. html和css入门

    html 单机软件:软件程序和数据都存储在客户端 界面:Tk.PyQt.wxPython库C/S(Client/Server)架构软件:软件程序和数据一部分存在客户端,一部分存在服务器端 界面:Tk. ...

  5. gulp的安装以及使用详解,除了详细还是详细

    安装gulp: 1. 创建本地包管理环境: 使用npm init命令在本地生成一个package.json文件,package.json是用来记录你当前这个项目依赖了哪些包,以后别人拿到你这个项目后, ...

  6. Node.js中的模块接口module.exports浅析

    在写node.js代码时,我们经常需要自己写模块(module).同时还需要在模块最后写好模块接口,声明这个模块对外暴露什么内容.实际上,node.js的模块接口有多种不同写法.这里作者对此做了个简单 ...

  7. hybird app混合开发介绍

    一 概念 1 Hybird App,是用现有前端(html,js,css)技术来开发的app.特点:1 灵活(开发灵活 ,部署灵活) 2 拥有类似原生的性能体验. 2 不是h5页面,也不是在webvi ...

  8. node-rsa非对称加密

    写在最前:此文的目的是介绍编码,减少刚接触时的弯路,所以内容且不做详细累述 一.使用 node-rsa 进行非对称加解密 因为 比特币 中使用的非对称加密,所以在npm中对比找到一个比较方便也直观的库 ...

  9. Eclipse个人规范化设置

    为保证在各个在各个系统中获得的代码样式保持一致,规范法化开发,对Eclipse进行一些常用通用设置: 1. 代码块缩进 4个空格,如果使用 tab缩进,请设置成 1个 tab为 4个空格.(阿里巴巴开 ...

  10. 再学Java 之 interface的成员变量

    前言:最近在学多线程,写“哲学家就餐问题(Dining Philosophers)”的时候,需要定义一个全局的变量,即哲学家的人数.常用的做法是在其中一个类中定义一个static final的变量,然 ...