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. 报错:Cannot remove entries from nonexistent file c:\program files\anaconda3\lib\site-packages\easy-install.pth

    Outline 这两天通过“掘金量化终端”跑模型策略,之前装好环境一直ok,可以顺畅的Running~ 下午重装了下 Anaconda,刚才跑的时候提示 缺少“gm”模块 (掘金量化必须包): 就按照 ...

  2. TimeQuest学习总结

    1. 基本时钟约束:creat_clock 2. 生成时钟约束:creat_generated_clock 3. I/O输入输出约束:(1)纯组合逻辑:set_max_delay & set_ ...

  3. web前端编码规范

    简要介绍 本文通过参考百度腾讯等前端编码规范(链接建文末),得出个人习惯的编码规范.个人编码规范采用在不影响可读性的情况下能省就省,尽量简洁,不需要就直接去掉. 最佳原则不管是个人编码规范还是团队编码 ...

  4. python16_day14【jQuery】

    一.作用域 1.作用域例一 <script> var str = "global"; //AO1 AO1.str function t(age){ console.lo ...

  5. python全栈开发从入门到放弃之网络基础

    一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...

  6. ul,li设置inline-block缝隙

    去掉换行符和空白符 margin-left: -0.5em(缝隙大小不确定) ul字号设为0,li设置字号 (有些浏览器设置最小字体) js移除空白子节点

  7. form:checkboxes radiobutton select用法

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

  8. CSS 控制元素 上下左右居中

    不说废话,直接 搞起..... 首先,我们将题目 <css控制元素上下左右居中> 分析一下哈,我是将其分成了4部分信息: 1.CSS控制: 只用 CSS 来达成目的 2.元素:  不只是d ...

  9. IIS DirectoryEntry

    DirectoryEntry是.Net给我们的一大礼物,他的名字我们就知道他的功能--目录入口.使用过ADSI的人都知道操作IIS,WinNT这些时,我们还需要提供他们的Path,操作IIS时,这个P ...

  10. HTTP与服务器的四种交互方式

    Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...