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. JavaScript 入门之常见对象

    常见对象 1. Object 对象 2. String 对象 3. Array 对象 4. Date 对象 5. Number 对象 6. 自定义对象 with 语句 为了简化对象调用内容的书写 格式 ...

  2. python通过原生sql查询数据库(共享类库)

    #!/usr/bin/python # -*- coding: UTF-8 -*- """DB共享类库""" # 使用此类,先实例化一个Da ...

  3. python collection 和 heapq 模块使用说明

    一 :集合库collection python 拥有一些内置的数据类型,collections模块提供啦几个额外的数据类型: 1,namedtuple   生成可以使用名字来访问元素内容的tuple子 ...

  4. Java集合类学习记录

    被标记为transient的属性在对象被序列化的时候不会被保存int[] arr1 = {1, 2, 3, 4, 5}; int[] arr2 = Arrays.copyOf(arr1, new_le ...

  5. android studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:

    今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...

  6. 使用 10046 查看执行计划并读懂 trace 文件

    查看 sql 执行计划的方法有许多种, 10046 事件就是其中的一种. 与其他查看 sql 执行计划不同, 当我们遇到比较复杂的 sql 语句, 我们可以通过 10046 跟踪 sql 得到执行计划 ...

  7. eclipse添加tomcat运行时

    方法一:添加jar包 方法二配置依赖 比如缺少javax.servlet.http.HttpServlet,ctrol+shift+t查找这个包 <dependencies> <de ...

  8. BZOJ 一句话题解

    菜鸡刷题记录 [题号:题解] 1008:简单排列组合 #include <bits/stdc++.h> using namespace std; #define ll long long ...

  9. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

  10. CF715E Complete the Permutations(第一类斯特林数)

    题目 CF715E Complete the Permutations 做法 先考虑无\(0\)排列的最小花费,其实就是沿着置换交换,花费:\(n-\)环个数,所以我们主要是要求出规定环的个数 考虑连 ...