poj 1787 Charlie's Change (多重背包可作完全背包)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3792 | Accepted: 1144 |
Description
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
Output
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. 感觉上是多重背包,实际上用完全背包的思路来做很快。 题意:分硬币,有1,5,10,25四种硬币,给定每种硬币的数量,给定要组合成的价值,问刚好达到价值时用的硬币最多的情况。 附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 10010
#define inf (-0x3f3f3f3f)
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int dp[N],path[N],used[N];
// dp[j] 表示 j 块钱最多由多少块硬币组成,
//path[j] 表示 上一次最多有多少块构成的 j 块钱,used[j] 表示 j 块钱时,已经放了多少同种类的硬币。
int i,j,m,n;
int num[],val[]= {,,,};
while(~scanf("%d %d %d %d %d",&n,&num[],&num[],&num[],&num[]))
{
if(n==&&num[]==&&num[]==&&num[]==&&num[]==)
break;
memset(dp,inf,sizeof(dp));
memset(path,,sizeof(path));
path[]=-;
dp[]=; for(i=; i<; i++)
{
memset(used,,sizeof(used));
for(j=val[i]; j<=n; j++)
{
if(dp[j-val[i]]+>dp[j]&&dp[j-val[i]]>=&&used[j-val[i]]<num[i])
{
dp[j]=dp[j-val[i]]+;
used[j]=used[j-val[i]]+;
path[j]=j-val[i];
}
}
} int ans[];
memset(ans,,sizeof(ans));
if(dp[n]<)
{
printf("Charlie cannot buy coffee.\n");
}
else
{
while(path[n]!=-)
{
ans[n-path[n]]++;
n=path[n];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[val[]], ans[val[]], ans[val[]], ans[val[]]);
}
}
return ;
}
poj 1787 Charlie's Change (多重背包可作完全背包)的更多相关文章
- POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)
网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...
- [POJ 1787]Charlie's Change (动态规划)
题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货 ...
- poj 1787 Charlie's Change
// 题意 给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1<=p&l ...
- POJ 1787 Charlie's Change
多重背包 可行性+路径记录 题意是说你要用很多其它的零钱去买咖啡.最后输出你分别要用的 1,5 ,10 .25 的钱的数量. 多重背包二进制分解.然后记录下 这个状态.最后逆向推就可以. #inclu ...
- poj 1787 背包+记录路径
http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Subm ...
- 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)
*注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- 多重背包转化成完全背包 E - Charlie's Change
http://poj.org/problem?id=1787 这个题目我一看就觉得是一个多重背包,但是呢,我不知道怎么输出路径,所以无可奈何,我就只能看一下题解了. 看了题解发现居然是把多重背包转化成 ...
- Charlie's Change POJ - 1787
Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...
随机推荐
- arcgis几何对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- arcgis增大缩放级别
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- FatMouse' Trade (贪心)
#include <iostream> #include <stdio.h> #include <cstring> #include <cmath> # ...
- update批量更新某一列成其它列对应的值【原】
update批量更新某一列成其它列对应的值 postgresql 标准sql语句 update AA set name = BB.name , AA.sex = BB.sex from BB wher ...
- python findall函数
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- FJWC2018
晚上水到8:40,感觉药丸. 把电脑带回寝室,大半夜敲键盘…… bzoj5254红绿灯 泰迪每天都要通过一条路从家到学校,这条路的起点是泰迪家,终点则是学校. 这条路中间还有n个路口,从第i-1个路口 ...
- PLAY2.6-SCALA(十一) 模板常用场景
1.布局 声明一个views/main.scala.html模板作为主布局模板 @(title: String)(content: Html) <!DOCTYPE html> <ht ...
- vs dump调试
1. dump文件和pdb文件的匹配问题 >> 发布二进制文件时生成的pdb文件一定要保留,只有当发布的二进制文件和pdb文件是同时生成的才好正确调试. 2. dump文件和pdb文件放在 ...
- 2019-10-31-C#-强转空会不会出现异常
title author date CreateTime categories C# 强转空会不会出现异常 lindexi 2019-10-31 8:53:6 +0800 2019-9-10 11:4 ...