Charlie's Change

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3176   Accepted: 913

Description

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.

Input

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.

Output

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.".

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(完全背包+路径记忆)的更多相关文章

  1. Charlie's Change_完全背包&&路径记录

    Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...

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

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  3. Charlie's Change(完全背包记录路径)

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  4. poj1787Charlie's Change(多重背包+记录路径+好题)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3720   Accepted: 1125 ...

  5. poj 1787 Charlie's Change (多重背包可作完全背包)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3792   Accepted: 1144 ...

  6. Charlie's Change POJ - 1787

    Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...

  7. B - Charlie's Change

    Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...

  8. poj1787 Charlie's Change

    Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...

  9. POJ 1787 Charlie's Change (完全背包/多重背包,输出方案的物品个数)

    网上说是多重背包,因为要输出方案,还要记录下路径,百度一下题解就可以. 自己做的时候,还没了解过多重背包,该题直接往完全背包思考了.咖啡的钱看作总的背包容量,1.5.10.25分别代表四种物品的重量, ...

随机推荐

  1. OCP新题,2019题库出现大量新题,062-第22题

    choose two Your database is running in ARCHIVELOG mode. You want to take a consistent whole database ...

  2. urllib2 的get请求与post请求

    urllib2默认只支持HTTP/HTTPS的GET和POST方法 urllib.urlencode() urllib和urllib2都是接受URL请求的相关参数,但是提供了不同的功能.两个最显著的不 ...

  3. ReactiveCocoa 源码阅读记录。

    1:RACSingle 需要订阅信号 RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACS ...

  4. mybatis 控制台打印执行的SQL语句

    1. Spring boot 集成 mybatis [转载]SpringBoot中Mybatis打印sql 1.1 如果使用的是application.properties文件,加入如下配置: log ...

  5. js转换数据格式为货币格式

    有时候输资金数据的时候如果位数较多就不好读了,如果输完能转换一下格式,转成用“,”隔开的通用格式就比较好看了.自己写了一个备用,以后用到的话就不用再写了. //将数字转换为货币格式,用,隔开 func ...

  6. vs 2017 IIS EXPRESS 增加局域网访问

    在VS调试站点,默认使用IISExpress,locall+端口,为了使用IP地址.多域名调试,找到 IISExpress下的applicationhost.config,在目标站点下增加类似行: & ...

  7. gitlab基本的命令

    1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v 添加远程仓库:$ git  ...

  8. jieba分词(2)

    结巴分词系统中实现了两种关键词抽取法,一种是TF-IDF关键词抽取算法另一种是TextRank关键词抽取算法,它们都是无监督的算法. 以下是两种算法的使用: #-*- coding:utf-8 -*- ...

  9. Hibernate的集合一对多与多对一

    需求:   部门与员工 一个部门有多个员工;       [一对多] 多个员工,属于一个部门    [多对一] 1.javaBean ——Dept.java package com.gqx.oneto ...

  10. 用鼠标键盘来控制你的Android手机——同屏显示简单教程

    今天在微博上看到有人用电脑鼠标操作iPhone手机玩打飞机游戏,非常炫,虽然自己用的不是iPhone,但相信Android手机肯定也能实现这样的功能,于是网上各种搜索方法,终于看到了一篇试用成功的帖子 ...