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

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,然后给出每一种硬币的个数,求买这个东西最多需要多少枚硬币

这题很不错,num[i][j]用来记录i状态下第j枚硬币的个数,dp[i]用来记录硬币的个数;要求的就是在硬币个数最多的情况下每一种硬币的个数

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
const int MAX = + ;
int dp[MAX],num[MAX][],c[],p;
int w[]={,,,};
void ZeroOnePage(int cost, int mount, int kind) //01背包传递参数有这一种硬币的数量和种类
{
for(int i = p; i >= cost; i--)
{
if(dp[i - cost] > - && dp[i] < dp[i - cost] + mount) //求枚数最多的情况,01的数量需要参数传递,完全背包就是1
{
dp[i] = dp[i - cost] + mount;
for(int j = ; j < ; j++)
num[i][j] = num[i - cost][j]; //更改这一状态下每一种硬币的数量
num[i][kind] += mount;
}
}
}
void CompletePage(int cost, int kind)
{
for(int i = cost; i <= p; i++)
{
if(dp[i - cost] > - && dp[i] < dp[i - cost] + )
{
dp[i] = dp[i - cost] + ;
for(int j = ; j < ; j++)
num[i][j] = num[i - cost][j];
num[i][kind] += ;
}
}
}
void MultiplePage(int cost,int mount,int kind)
{
if(cost * mount >= p)
{
CompletePage(cost, kind);
return;
}
int k = ;
while(k < mount)
{
ZeroOnePage(k * cost, k, kind);
mount = mount - k;
k <<= ;
}
if(mount > )
ZeroOnePage(mount * cost, mount, kind);
return ;
}
int main()
{
while(scanf("%d", &p) != EOF)
{
int sum = p;
for(int i = ; i < ; i++)
{
scanf("%d", &c[i]);
sum += c[i];
}
if(sum == )
break;
memset(num,,sizeof(num));
memset(dp,-,sizeof(dp));
dp[] = ;
for(int i = ; i < ; i++)
if(c[i])
MultiplePage(w[i],c[i],i);
if(dp[p] > )
{
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",num[p][],num[p][],num[p][],num[p][]);
}
else
{
printf("Charlie cannot buy coffee.\n");
}
}
return ;
}

poj1787Charlie's Change(多重背包+记录路径+好题)的更多相关文章

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

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

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

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

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

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

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

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

  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. hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)

    题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活                                   ...

随机推荐

  1. 细说git merge & git rebase

    git merge和git rebase两个都是用来合并两个分支用的,在使用过程中,这两个概念容易混淆. 在此,对这两个git技巧的用法进行详细描述,希望能帮助一些热爱git的朋友. -------- ...

  2. 无法解析此远程名称: 'www.***.com' 解决办法 请求因 HTTP 状态 417 失败

    今天在做接口开发时,遇到了一个异常:无法解析此远程名称: 'www.***.com'.我的网站一直是运行正常的,从昨天开始出现异常,用户可以使用,但我的服务器怎么也无法实现对数据库的更新. 分析原因: ...

  3. Linux 读书笔记 二

    一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 若不小心登出后,直接刷新页面即可 2. 环境使用 完成实验后可以点击桌面上方的“实验截图”保存并分 ...

  4. wen7安装oracle 11g出现"未找到文件 E:\development_tools\database\oracle\install_d\dbhome\owb\external\oc4j_applications\applications\WFMLRSVCApp.ear"

    从oracle官网上下载了window7 64位的oracle安装包win64_11gR2_database_1of2,安装后出现了错误: 解决方法:继续下载oracle官网上的文件2:win64_1 ...

  5. VmWare Workstation 10 安装 Ubuntu 14.04 问题解决

    Ubuntu安装过程很顺利,安装完成后还是有小问题存在   问题1:无法联网,PING可以通,网址无法解析 原因:默认DNS设置不正确 解决:设置DNS地址为8.8.8.8,问题解决   问题2:vm ...

  6. 无光驱安装原版 windows server2008,win7 的方法,64位的。

    这几天要对一台服务器进行安装 windows server2008的系统,64位.尼玛在网上买了一个光驱迟迟不到所以只能用U盘来了 以前安装ghost的系统U盘分分钟搞定.安装原版的iso文件遇到了一 ...

  7. 【hello,world 也打脸】记storm-starter在某知名IDE下的悲催调试经历

    背景 最近收到这样一个问题: Storm处理消息时会根据Topology生成一棵消息树,Storm如何跟踪每个消息.如何保证消息不丢失以及如何实现重发消息机制? 虽已回复,但心想还是看下storm这块 ...

  8. DateTime.Parse

    上月第一天:DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01")) 上周星期天:DateTime.Par ...

  9. grunt安装

    随着node的流行,各种后台的技术应用到前端,依赖注入.自动化测试.构建等等. 本篇就介绍下如何使用Grunt进行构建. grunt安装 由于grunt依赖于nodejs,因此需要先安装nodejs. ...

  10. Daily Scrum – 1/15

    Meeting Minutes 确定了user course 的方案. 完成了屏幕的自适应: 安排了最后几天的日程 Burndown     Progress   part 组员 今日工作 Time ...