http://poj.org/problem?id=1787
 
描述
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.


输入
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.输出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.".
样例输入

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

样例输出

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.

 
对于这题说法不一, 有人说是多重背包, 有人说是完全背包, 反正我是当多重背包看了, 但是看看人家写的完全背包还是不难理解的, 挺巧妙的
自我感觉第二种解法更方便一些
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;#define met(a,b) (memset(a,b,sizeof(a)))
#define N 11000
#define INF 0x3f3f3f3fint pre[N], v[5]={0,1,5,10,25}, dp[N];
int used[N];/**dp[i]   代表组成i元时需要的最多的硬币
used[i] 代表第i种硬币用了几次(感觉这点很好,能将多重背包转化为完全背包)
pre[j]  记录的是j从哪个状态转化过来的*/int main()
{int p, num[5]={0};while(scanf("%d%d%d%d%d", &p, &num[1], &num[2], &num[3], &num[4]), p+num[1]+num[2]+num[3]+num[4]){int i, j, ans[110]={0};met(dp, -1);met(pre, -1);dp[0] = 0;for(i=1; i<=4; i++){memset(used, 0, sizeof(used));for(j=v[i]; j<=p; j++){if(dp[j-v[i]]+1>dp[j] && dp[j-v[i]]>=0 && used[j-v[i]]<num[i]){dp[j] = dp[j-v[i]]+1;used[j] = used[j-v[i]]+1;pre[j] = j-v[i];}}}if(dp[p]<0)printf("Charlie cannot buy coffee.\n");else{met(ans, 0);i = p;while(1){if(pre[i]==-1) break;ans[i-pre[i]]++;i = pre[i];}printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[v[1]], ans[v[2]], ans[v[3]], ans[v[4]]);}}return 0;
}

另一种解法:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>using namespace std;
#define met(a,b) (memset(a,b,sizeof(a)))
const int N=10001;
int dp[N], v[5]={0,1,5,10,25};
int num[N][5];//dp[j]为咖啡的价格为j时,所能花费的最多钱币数//num[j][i],表示咖啡的价格为j时,花费的第i种货币的个数
int main()
{int a[5], p;while(scanf("%d%d%d%d%d", &p, &a[1], &a[2], &a[3], &a[4]), p+a[1]+a[2]+a[3]+a[4]){int i, j, k;met(dp, -1);met(num, 0);dp[0] = 0;for(i=1; i<=4; i++){for(j=v[i]; j<=p; j++){if(dp[j-v[i]]>=0 && dp[j-v[i]]+1>dp[j] && num[j-v[i]][i]<a[i]){dp[j] = dp[j-v[i]] + 1;for(k=1; k<=4; k++){if(k==i)num[j][k] = num[j-v[i]][k]+1;elsenum[j][k] = num[j-v[i]][k];}}}}if(dp[p]==-1)printf("Charlie cannot buy coffee.\n");elseprintf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", num[p][1], num[p][2], num[p][3], num[p][4]);}return 0;
}

转载于:https://www.cnblogs.com/YY56/p/5539617.html

(多重背包+记录路径)Charlie's Change (poj 1787)相关推荐

  1. ☆【UVA - 624 】CD(dp + 0-1背包 + 记录路径)

    题干: You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music ...

  2. 【01背包记录路径】东东开车了

    题面 东东开车出去泡妞(在梦中),车内提供了 n 张CD唱片,已知东东开车的时间是 n 分钟,他该如何去选择唱片去消磨这无聊的时间呢 假设: CD数量不超过20张 没有一张CD唱片超过 N 分钟 每张 ...

  3. 欠债还钱、Codeforces Round #637 (Div. 2) -D(多重背包)

    Description llk经常和wy一起去yh小饭馆吃盖浇饭,一天他们吃完后llk把两个人的钱一起付了,但是wy不想欠llk的钱.现在wy手中有一些散钱,llk手中也有一些散钱,wy想知道能不能刚 ...

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

    题目:点击打开链接 题意: 给出了一杯咖啡多少钱,和1块,5块,10块,25块各有多少,要正好买这杯咖啡,并且花的硬币越多越好. 思路: 简单的多重背包,类似poj2392,不用二进制优化,恰好买这杯 ...

  5. 01背包输出路径、完全背包、多重背包

    背包问题 一.01 Knapsack(输出路径- >选的物品) 二.完全背包 1.三重循环,极可能TLE,滚动数组优化后j逆向枚举 2.二重,优化消去变量k(没有特别厘清,但可以直接从完全背包角 ...

  6. POJ 1742 Coins ( 经典多重部分和问题 DP || 多重背包 )

    题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到 ...

  7. Lightoj 1231 - Coin Change (I) (裸裸的多重背包)

    题目链接: Lightoj  1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...

  8. poj 3260 The Fewest Coins(多重背包+完全背包)

    http://poj.org/gotoproblem?pid=3260 (1)多重背包的处理方式:转化为分组背包(1,2,4,8,余数).具体细节参见代码: scanf("%d", ...

  9. POJ - 2392 朴素多重背包 + 贪心 WA与AC代码细节分析

    我们先来看下普通的朴素多重背包(拆成01背包求解) n种物品,背包大小w,每种物品重量 wi,价值 vi,个数 ci dp[j] 表示 大小为 j 的背包含有的最大价值,即 物品重量和 小于等于 j ...

最新文章

  1. Require.js
  2. (Java)常用排序
  3. centos 日志审计_Linux\CentOS中auditd安全审计工具的使用
  4. 追加 java,收藏 java追加写文件的方法
  5. Linux下文件的三个时间意义及用法
  6. 数据结构的简单理解(4)
  7. 人工玻璃体与交联反应调研
  8. GCD简介一:基本概念和Dispatch Queue
  9. 基于JAVA+SpringMVC+MYSQL的求职招聘管理系统
  10. 自定义训练中使用Tensorboard
  11. 从ISO 文件制作U盘启动盘.
  12. 命令行调用SQL查询分析器
  13. vs 输入代码时出现火花_VSV和VBV随发动机转速和进气温度怎么变化维修执照机务在线...
  14. 江苏小高考计算机什么时候考,2021江苏小高考时间 什么时候考试
  15. beyondcompare ubuntu revoked问题
  16. 74LVC245电平转换电路
  17. c#语言-正方形,圆形,利用接口实现周长及面积的计算
  18. 全国计算机等级考试二级公共,全国计算机等级考试二级公共基础知识.
  19. 2021 安装centos
  20. FATE —— 二.3.2 Hetero-NN使用CustModel设置顶部、底部模型

热门文章

  1. oracle常用的知识点
  2. qt如何安装python_安装Python QT,PythonQT,的
  3. 查询商品信息报错FreeMark template error
  4. TF1与TF2的求和程序对比
  5. 2019年Java程序设计讲课笔记目录
  6. 【codevs1230】元素查找,弱弱的二分查找
  7. 全国计算机考试一级在线模拟,2017全国计算机一级考试模拟
  8. 2017.9.26 于神之怒加强版 失败总结
  9. 2017.9.10 ricehub 思考记录
  10. 【Level 08】U08 Positive Attitude L2 Into the world of a bookworm