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. Mahout学习路线图-张丹老师

    前言 Mahout是Hadoop家族中与众不同的一个成员,是基于一个Hadoop的机器学习和数据挖掘的分布式计算框架.Mahout是一个跨学科产品,同时也是我认为Hadoop家族中,最有竞争力,最难掌 ...

  2. PAT 天梯赛 L1-044. 稳赢 【循环】

    题目链接 https://www.patest.cn/contests/gplt/L1-044 题意 根据对方给出剪刀石头步 给出对应的胜利出招,每K次 要有一个平局 思路 用一个循环,然后每K次 判 ...

  3. Hive2.2.1概述(待重写)

    概述 hive 是一个包裹着 hdfs 的壳子,hive 通过 hql,将 sql 翻译成 MR ,进行数据查询. Hive是⼀个构建在Hadoop之上的数据仓库 hive的数据存在hdfs上,元信息 ...

  4. CWinApp类CMultiDocTemplate类CDocument类CView类的关系

    转自:http://blog.csdn.net/bboot/article/details/26884011 不得不转,瞬间搞清了很多问题,短小精悍 1.CWinApp类   它包含并管理着应用程序的 ...

  5. Spark机器学习8· 文本处理(spark-shell)

    Spark机器学习 自然语言处理(NLP,Natural Language Processing) 提取特征 建模 机器学习 TF-IDF(词频 term frequency–逆向文件频率 inver ...

  6. 利用ST MCU内部的基准参考电压监测电源电压及其它

    源: 利用ST MCU内部的基准参考电压监测电源电压及其它

  7. 【Thinking in java, 4e】复用类

    mark一篇09年的<Thinking in Java>笔记:here --> https://lawrence-zxc.github.io/2009/11/07/thinking- ...

  8. Apache 错误整理

    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localho ...

  9. MLP(多层神经网络)介绍

    写在前面的 接触神经网络(ANN)的时间很长了,以前也只是学了学原理,做过一个BPN的练习,没有系统的总结过,最近看Torch的源码,对MLP有了更多的了解,写写自己学到的东西吧,算是做了一次总结! ...

  10. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...