Charlie's Change(完全背包+路径记忆)
Charlie's Change
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 3176 | Accepted: 913 |
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的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1<=p<=1万,1<=硬币数量<=1万
思路:完全背包+路径记忆。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int v[]= {,,,};
int dp[];
int path[],used[];
int num[];
int ans[];//这个要大于25
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int P;
while(scanf("%d%d%d%d%d",&P,&num[],&num[],&num[],&num[]),(P+num[]+num[]+num[]+num[]))
{
for(int i=; i<=P; i++)dp[i]=-INF;
memset(path,,sizeof(path)); path[]=-;//退出的条件
dp[]=;
for(int i=; i<; i++)
{
memset(used,,sizeof(used));
for(int j=v[i]; j<=P; j++)
{
if(dp[j-v[i]]+>dp[j]&&dp[j-v[i]]>=&&used[j-v[i]]<num[i])
{
//dp[j]存j里用多少枚硬币换。(要多的);used来记录是否超过数量
dp[j]=dp[j-v[i]]+;//压入dp,
used[j]=used[j-v[i]]+;//体积j,里数量加一
path[j]=j-v[i];//记录上一个路径(体积)
}
// printf("dp[%d]=%d \t used[%d]=%d\tpath[%d]=%d\n",j,dp[j],j,used[j],j,path[j]);
}
} if(dp[P]<)
{
printf("Charlie cannot buy coffee.\n");
continue;
} memset(ans,,sizeof(ans));
int i=P;
while()
{
if(path[i]==-)break;
ans[i-path[i]]++;
i=path[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[v[]],ans[v[]],ans[v[]],ans[v[]]); }
return ;
}
/*
12 5 3 1 2
16 0 0 0 1
0 0 0 0 0
*/
不懂看看测试数据;
是不是很清楚了。。哈哈。
Charlie's Change(完全背包+路径记忆)的更多相关文章
- Charlie's Change_完全背包&&路径记录
Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- Charlie's Change(完全背包记录路径)
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...
- poj1787Charlie's Change(多重背包+记录路径+好题)
Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3720 Accepted: 1125 ...
- poj 1787 Charlie's Change (多重背包可作完全背包)
Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3792 Accepted: 1144 ...
- Charlie's Change POJ - 1787
Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...
- B - Charlie's Change
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...
- poj1787 Charlie's Change
Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...
- POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)
网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...
随机推荐
- 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar
[源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar 作者:webabcd 介绍背水一战 Windows 10 之 控件(导航类) App ...
- Linux Compile Multiple C++ Files
Compile Two Files: $ CC -c Main.cc Sales_item.cc # by default generates a.exe # some compilers gener ...
- 【BZOJ3238】 [Ahoi2013]差异(SAM)
传送门 BZOJ 洛谷 Solution SA版本的 考虑可以建一个SAM? 那么接下来我们就考虑每一对点对之间的贡献了. 把这个式子化简一下就是无序点对之间的那啥(自己意会一下) 然后我们定义边权为 ...
- OCP新题,2019题库出现大量新题,062-第22题
choose two Your database is running in ARCHIVELOG mode. You want to take a consistent whole database ...
- Linux正则与文本处理工具(10)
正则表达式 (Regular Expression, RE, 或称为常规表达式)是通过一些特殊字符的排列,用于『查找/替换/删除』一行或多行文字或字符串,简单的说,正则表达式就是用在字串的处理上面的一 ...
- BZOJ 1011--[HNOI2008]遥远的行星(乱搞)
1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 5684 Solved ...
- SpringBoot配置文件application.properties详解
喜欢的朋友可以关注下,粉丝也缺. 相信很多的码友在接触springboot时,不知道怎么去配置一些项目中需要的配置,比如数据源,tomcat调优,端口等等,下面我就给大家奉献出一些项目中常用的配置信息 ...
- 精通CSS 第1章
一 标记简史 1 使用有意义的元素 2 ID和类名:ID是唯一的,而一个类名可以应用于多个元素.在写ID和类名时需要注意区分大小写,并使用统一的命名约定,比如完全小写+连字符分割,例andy-budd ...
- 轮播图采用js、jquery实现无缝滚动和非无缝滚动的四种案例实现,兼容ie低版本浏览器
项目源代码下载地址:轮播图 以下为项目实现效果:(由于gif太大,所以只上传一张图片,但效果完全能实现,经测试,在ie各版本浏览器及chrome,firefox等浏览器中均能实现效果,可以实现点击切换 ...
- ajex 相关参数
1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...
