poj1787Charlie's Change(多重背包+记录路径+好题)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3720 | Accepted: 1125 |
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.
题意:给出想要买的东西的价格p,有四枚硬币面值分别是1,5,10,25,然后给出每一种硬币的个数,求买这个东西最多需要多少枚硬币
这题很不错,num[i][j]用来记录i状态下第j枚硬币的个数,dp[i]用来记录硬币的个数;要求的就是在硬币个数最多的情况下每一种硬币的个数
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
const int MAX = + ;
int dp[MAX],num[MAX][],c[],p;
int w[]={,,,};
void ZeroOnePage(int cost, int mount, int kind) //01背包传递参数有这一种硬币的数量和种类
{
for(int i = p; i >= cost; i--)
{
if(dp[i - cost] > - && dp[i] < dp[i - cost] + mount) //求枚数最多的情况,01的数量需要参数传递,完全背包就是1
{
dp[i] = dp[i - cost] + mount;
for(int j = ; j < ; j++)
num[i][j] = num[i - cost][j]; //更改这一状态下每一种硬币的数量
num[i][kind] += mount;
}
}
}
void CompletePage(int cost, int kind)
{
for(int i = cost; i <= p; i++)
{
if(dp[i - cost] > - && dp[i] < dp[i - cost] + )
{
dp[i] = dp[i - cost] + ;
for(int j = ; j < ; j++)
num[i][j] = num[i - cost][j];
num[i][kind] += ;
}
}
}
void MultiplePage(int cost,int mount,int kind)
{
if(cost * mount >= p)
{
CompletePage(cost, kind);
return;
}
int k = ;
while(k < mount)
{
ZeroOnePage(k * cost, k, kind);
mount = mount - k;
k <<= ;
}
if(mount > )
ZeroOnePage(mount * cost, mount, kind);
return ;
}
int main()
{
while(scanf("%d", &p) != EOF)
{
int sum = p;
for(int i = ; i < ; i++)
{
scanf("%d", &c[i]);
sum += c[i];
}
if(sum == )
break;
memset(num,,sizeof(num));
memset(dp,-,sizeof(dp));
dp[] = ;
for(int i = ; i < ; i++)
if(c[i])
MultiplePage(w[i],c[i],i);
if(dp[p] > )
{
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",num[p][],num[p][],num[p][],num[p][]);
}
else
{
printf("Charlie cannot buy coffee.\n");
}
}
return ;
}
poj1787Charlie's Change(多重背包+记录路径+好题)的更多相关文章
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- 01背包记录路径 (例题 L3-001 凑零钱 (30分))
题意: 就是找出来一个字典序最小的硬币集合,且这个硬币集合里面所有硬币的值的和等于题目中的M 题解: 01背包加一下记录路径,如果1硬币不止一个,那我们也不采用多重背包的方式,把每一个1硬币当成一个独 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- 完全背包记录路径poj1787 好题
这题有点多重背包的感觉,但还是用完全背包解决,dp[j]表示凑到j元钱时的最大硬币数,pre[j]是前驱,used[j]是凑到j时第i种硬币的用量 △回溯答案时i-pre[i]就是硬币价值 #incl ...
- poj 1787 背包+记录路径
http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Subm ...
- 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径
链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...
- UVA 624(01背包记录路径)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA624(01背包记录路径)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)
题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 ...
随机推荐
- RelayCommand命令
原文:http://www.cnblogs.com/xiepeixing/archive/2013/08/13/3255152.html 常用Wpf开发中我们在ViewModel中实现INotifyP ...
- Windows下安装Redmine
参考链接:http://www.cnblogs.com/afarmer/archive/2011/08/06/2129126.html 最新教程:http://www.myexception.cn/w ...
- Sublime Text 3 文本编辑器
1.安装下载 下载地址:http://www.cr173.com/soft/121149.html http://www.xiazaiba.com/html/24343.html 官网 http:// ...
- [DE2i-150] 重建PCIe_Fundmental範例說明
以下資料的整理主要是做備忘錄,避免以後忘了,順便留給需要的人. ========================================== 本文主要是參考友晶科技的DE2i-150光碟裡面的 ...
- C语言 百炼成钢18
//题目52:用递归打印以下图形 //* //*.*. //*..*..*.. //*...*...*...*... //*....*....*....*....*.... #include<s ...
- GPS坐标换算为百度坐标
最近在做一个关于手机定位的小应用,需求是这样的,用户通过手机(Wp8)进行二维码扫描操作并且记录用户的当前位置,在PC上可以查看用户所在地图的位置,做法就是在用户扫描条码时,通过手机GPS获取当前在地 ...
- iOS 定位精度
时间 2015-03-19 18:30:59 图灵社区 由于iOS不能直接控制到GPS,一般来说我们都会使用CLLocationManager来获取地理位置信息,我们会使用 manager.des ...
- canvas学习笔记:小小滴公式,大大滴乐趣
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了. ...
- Jenkins进阶之自动发送邮件的Default Content设置模板
分享一个简洁实用的Jenkins项目邮件管理系统的"Default Content"设置模板 配置如下: <h1><center><font colo ...
- 初识 swift 封装轮播图
一.简介 换了一家公司.换了一个环境刚开始来公司自然不能有一丝一毫的放松,每天即使是没有什么工作也是看看这个博客.那个源码.尽量让自己更充实.慢慢的开始写几篇博客记录下自己遇到的一些问题和解决方法.其 ...