http://poj.org/problem?id=1787

Charlie's Change
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4512   Accepted: 1425

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.

Source

  给出了四种不同面值的硬币及其数量,问组成P价值所用的最多数量的硬币是多少,并输出这个方案。应该是多重背包把这个,用二进制优化个数之后就不会T了,刚开始卡了很久因为两层循环我写反了,一直没写过背包有点蒙,由于用到了一维数组的优化,所以对于当前的某件物品,当前的价值用到的子问题必须不涉及到这件物品。

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
int coin[]={,,,,};
int f[];
int book[];
struct date
{
int num,type;
}Q[];
int main()
{
int C[],P,n,m,i,j,k;
while(cin>>P>>C[]>>C[]>>C[]>>C[]){
if(!(P+C[]+C[]+C[]+C[])) break;
memset(f,-inf,sizeof(f));
memset(Q,,sizeof(Q));
memset(book,,sizeof(book));
int W,L,l=;
f[]=;
for(i=;i<=;++i)
{
l=;
for(k=;C[i];C[i]-=k,k*=){
if(C[i]<k) k=C[i];
// cout<<i<<' '<<k<<endl;
W=k*coin[i];
for(j=P;j>=W;--j)
{ if(f[j-W]!=-inf&&f[j]<f[j-W]+k)
{
f[j]=f[j-W]+k;
Q[j].num=k;
Q[j].type=i;
}
}
}
}//puts("dd");
// cout<<f[P]<<endl;
if(f[P]<) puts("Charlie cannot buy coffee.");
else{
j=P;
i=;
while(j){
book[Q[j].type]+=Q[j].num;
j-=coin[Q[j].type]*Q[j].num;
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",book[],book[],book[],book[]);
}
}
return ;
}

poj 1787 背包+记录路径的更多相关文章

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

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

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

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

  3. 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...

  4. POJ 3414 Pots 记录路径的广搜

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  5. UVA 624(01背包记录路径)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVA624(01背包记录路径)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

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

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

  9. HDU 6083 度度熊的午饭时光(01背包+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...

随机推荐

  1. XPath 快速入门

    XPath 是一门在 xml 文档中查找信息的语言. XPath 使用路径表达式来选取 xml 文档中的节点或者节点集合. 路径表达式由元素和属性组成. 语法介绍 // 示例: // xml 文档 & ...

  2. SQL Server 2005 临时表

    with t as ( select * from t_pub_param ) select * from t SQL Server 2005 之后才可以使用,查询后临时表t会自动删除.

  3. window下安装mysql详细步骤

    1.下载安装包 打开mysql官网下载页面:http://dev.mysql.com/downloads/mysql/ 1.选择相应的版本和平台 2.mysql配置 打开刚刚解压的文件夹F:\mysq ...

  4. MySQL(单表的表记录的操作)

    一.表记录的增删改查 1.增加表记录 <1>插入一条记录: insert [into] tab_name (field1,filed2,.......) values (value1,va ...

  5. R中的data.table 快速上手入门

    data.table包提供了一个非常简洁的通用格式:DT[i,j,by]. 可以理解为:对于数据集DT,选取子集行i,通过by分组计算j. 对比与dplyr等包,data.table的运行速度更快. ...

  6. centos7 vmware克隆解决网络问题

    centos7克隆后,发现两台机子之间的MAC相同,无法获取IP.超找资料,解决办法如下: 只要删除网卡的配置文件中的HWADDR和UUID两行就行.使用ifup启动网卡即可.

  7. T25健身视频全集+课表

    http://jianfei.39.net/thread-3639251-1.html T25健身视频全集+课表 强度适中 不伤膝盖! [复制链接]     zytttt         主题 好友 ...

  8. form:checkboxes radiobutton select用法

    <form:checkboxes path="subjects" items="${requestScope.subjects}" element=&qu ...

  9. Python框架之Tornado (源码之褪去模板外衣)

    上一篇介绍了客户端请求在tornado框架中的生命周期,其本质就是利用epoll和socket来获取并处理请求.在上一篇的内容中,我们只是给客户端返回了简单的字符串,如:“Hello World”,而 ...

  10. $python正则表达式系列(6)——"或"表达式的用法

    import re s1 = u'距离地铁5号线189米' s2 = u'距离地铁5号线(环中线)189米' s3 = u'距离地铁5号线(环中线)189米' p1 = re.compile(u'号线 ...