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分别代表四种物品的重量, ...
随机推荐
- 【转】selenium webdriver三种等待方法
原文:https://www.cnblogs.com/lgh344902118/p/6015593.html webdriver三种等待方法 1.使用WebDriverWait from seleni ...
- Linux 操作系统文件略解
1.使用tree命令查看根目录的树结构 # tree -L 1 如果没有tree命令,可以使用yum进行安装 # yum -y install tree 执行命令后,即可看到根下一共有19个目录 . ...
- nginx root&alias 文件路径配置
nginx 指定文件路径有两种方式 root 和 alias,root 与 alias 主要区别在于 nginx 如何解释 location 后面的 uri,这会使两者分别以不同的方式将请求映射到服务 ...
- [JavaScript] 根据指定宽度截取字符串
/** * 根据指定宽度截取字符串 * @param desc 原始字符串 * @param width 该显示的宽度 * @param fontsize 字体大小 12px * @returns { ...
- Spring boot mybatis : Error creating bean with name 'com.github.pagehelper.autoconfigure.MapperAutoConfiguration': Invocation of init method failed;
报错截图: 解决方法: 只能扫描到自定义的mapper,不能扫描到其他文件. @MapperScan("com.streamax.s17.tms.dao.pper.repository&qu ...
- 简单介绍RPM包制作方法
RPM是RedHat Package Manager(RedHat软件包管理工具)的缩写,是一种用于互联网下载包的打包及安装工具,它包含在某些Linux分发版中.它生成具有.RPM扩展名的文件.使用r ...
- VS2017中使用Git进行版本控制
简单介绍在VS2015下使用Git来管理项目 1. VS2017启用Git源代码管理插件 第一步,打开vs: 第二步,打开[团队资源管理器]: 第三步,选择团队管理器选项卡下的[连接]页面,点击[克隆 ...
- 【sping揭秘】8、容器内部事件发布(一)
容器内部事件发布 Spring的applicationContext容器提供的容器内事件发布功能,是通过java提供的自定义事件实现的 事件类型:eventObject 类继承 事件监听:eventL ...
- 分享:基于Dracula+Zenburn 自定制的pycharm主题配色文件
显示效果: PS:彩色配色,一是可以提高平时写代码的乐趣,另一个是,对视力相对比较好. 配置方法: 1. 在你本地的操作系统里,找到pycharm安装的时候,默认在C盘创建的文件夹colors:C:\ ...
- 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 0、学习目标
1. Build a logistic regression model, structured as a shallow neural network2. Implement the main st ...