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. table标签去除默认边框

    table去除默认边框 1.在没有出去默认边框时,改变底部颜色,依然显示1px左右的白色边框 2.为table 加上 border="0" cellpadding="0& ...

  2. iOS9新特性——堆叠视图UIStackView

    一.引言 随着autolayout的推广开来,更多的app开始使用自动布局的方式来构建自己的UI系统,autolayout配合storyBoard和一些第三方的框架,对于创建约束来说,已经十分方便,但 ...

  3. GridPanel中getSelectionModel

    GridPanel中getSelectionModel 更多 2014/5/1 来源:extjs学习浏览量:6783 学习标签: GridPanel extjs 本文导读:Ext.grid.GridP ...

  4. 最短路径—Dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  5. WP 8.1 中挂起时页面数据保存方式

    1.保存到Applicaion Data配置信息中: 保存: private void testTB_TextChanged(object sender, TextChangedEventArgs e ...

  6. Silverlight OOB程序签名问题

    浏览器外部署Silverlight时,为了让部署到本地的Silverlight应用程序保持最新,通常需要在应用程序中添加更新检查的功能.具体实现可参见这儿. 除了文中提到的“应用程序中使用了用户尚未安 ...

  7. 通信vue2.0组件

    vue2.0组件通信各种情况总结与实例分析   Props在vue组件中各种角色总结 在Vue中组件是实现模块化开发的主要内容,而组件的通信更是vue数据驱动的灵魂,现就四种主要情况总结如下: 使用p ...

  8. C# 杂项

    1,函数访问等级必须高于参数等级,如函数等级是PUBLIC,则参数必须高于等于PUBLIC,若为INTERNAL 则不行.INTERNAL 低于PUBLIC, 用于同一个程序集内引用,PUBLIC则可 ...

  9. ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求

    大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...

  10. BigDecimal 使用方法详解

    BigDecimal 使用方法详解 博客分类: java基础 bigdecimalmultiplyadddivide  BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (sca ...