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.

这题可以用多重背包,用dp[m][5]分别记录m价钱刚好能买到的物品个数的最大值,买1元,5元,10元,25元物品的个数。

#include<stdio.h>
#include<string.h>
#define inf 88888888
int max(int a,int b){
return a>b?a:b;
}
int w[10]={0,1,5,10,25},v[10]={1,1,1,1,1,1};
int dp[10006][6];
int main()
{
int n,m,i,j,a,b,c,d,k,ans,sum;
int num[19];
while(scanf("%d%d%d%d%d",&m,&num[1],&num[2],&num[3],&num[4])!=EOF)
{
if(m+num[1]+num[2]+num[3]+num[4]==0)break;
if(m>num[1]+num[2]*5+num[3]*10+num[4]*25){
printf("Charlie cannot buy coffee.\n");continue;
}
if(num[1]>=m){
printf("Throw in %d cents, 0 nickels, 0 dimes, and 0 quarters.\n",m);continue;
}
for(i=0;i<=m;i++){
dp[i][0]=-inf;
}
dp[0][0]=dp[0][1]=dp[0][2]=dp[0][3]=dp[0][4]=0; for(i=1;i<=4;i++){
ans=num[i]*w[i];
if(ans>=m){
for(j=w[i];j<=m;j++){
if(dp[j-w[i]][0]>=0){
if(dp[j-w[i]][0]+v[i]>dp[j][0]){
dp[j][0]=dp[j-w[i]][0]+v[i];
dp[j][1]=dp[j-w[i]][1];dp[j][2]=dp[j-w[i]][2];dp[j][3]=dp[j-w[i]][3];dp[j][4]=dp[j-w[i]][4];
dp[j][i]++; }
}
}
}
else{
k=1;sum=0;
while(sum+k<num[i]){
sum+=k;
for(j=m;j>=k*w[i];j--){
if(dp[j-k*w[i]][0]>=0){
if(dp[j-k*w[i]][0]+k*v[i]>dp[j][0]){
dp[j][0]=dp[j-k*w[i]][0]+k*v[i];
dp[j][1]=dp[j-k*w[i]][1];dp[j][2]=dp[j-k*w[i]][2];dp[j][3]=dp[j-k*w[i]][3];dp[j][4]=dp[j-k*w[i]][4];
dp[j][i]+=k; }
} }
k=k*2;
}
k=num[i]-sum;
for(j=m;j>=k*w[i];j--){
if(dp[j-k*w[i]][0]>=0){
if(dp[j-k*w[i]][0]+k*v[i]>dp[j][0]){
dp[j][0]=dp[j-k*w[i]][0]+k*v[i];
dp[j][1]=dp[j-k*w[i]][1];dp[j][2]=dp[j-k*w[i]][2];dp[j][3]=dp[j-k*w[i]][3];dp[j][4]=dp[j-k*w[i]][4];
dp[j][i]+=k; }
} }
}
}
if(dp[m][0]>0)
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",dp[m][1],dp[m][2],dp[m][3],dp[m][4]);
else printf("Charlie cannot buy coffee.\n");
//printf("%d\n",dp[m][0]);
}
return 0;
}

poj1787 Charlie's Change的更多相关文章

  1. poj1787 Charlie's Change

    思路: 完全背包,记录路径. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; ] ...

  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's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 D ...

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

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

  5. Charlie's Change POJ - 1787

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

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

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

  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. [POJ 1787]Charlie's Change (动态规划)

    题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货 ...

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

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

随机推荐

  1. Memcached、Redis、Mongodb比较

    Memcached(内存Cache) Memcached 是一个高性能的分布式内存对象缓存系统.通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库 ...

  2. 【Oracle】Oracle中chr()的含义

    oracle中chr含义 CHR(10)和 CHR(13)--在oracle都为换行 chr(32)--表示空格 DECLARE v_a VARCHAR2(255); v_b VARCHAR2(255 ...

  3. .NET 云原生架构师训练营(模块二 基础巩固 Scrum 团队)--学习笔记

    2.7.3 Scrum 团队 理想的环境 团队章程 如何组建 Scrum 团队 产品待办事项列表 用户故事 敏捷开发流程 理想的环境 5-9人 100% 跨职能 在一起 自组织 自组织 目标 授权 沟 ...

  4. paramiko模块简单用法

    最简单最基本的用法 1 #__*__coding:utf-8__*__ 2 import paramiko 3 hostname = '192.168.1.1' 4 username = 'root' ...

  5. 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null

    之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...

  6. Inlook - 你的私人工作助理 V1.0.0.2

    Inlook - Your personal assistant 中文版|English version Introduction Inlook是为在桌面上直观地提醒用户收到未读邮件和日程安排而开发的 ...

  7. DB2版本升级(V9.7升级到V11.1)

    1.V11.1版本升级路线 DB2 11.1 可以将现有的 Db2 V9.7.Db2 V10.1 或 Db2 V10.5 实例和数据库直接升级到 Db2 V11.1.如果 Db2 服务器正在 Db2 ...

  8. JCO RFC destination

    一:登陆PI的GUI,进入事物SM59,创建T类型RFC destinations如下: AI_RUNTIME_JCOSERVER  :used for the mapping runtime, va ...

  9. Java基础复习3

    循环的嵌套 public class demo8 {     public static void main(String[] args) {         /*  输出########       ...

  10. 关于springboot项目通过jar包启动之后无法读取项目根路径静态资源

    在一次项目开发过程中,项目根路径下存放了一张图片,生成二维码的时候调用了该图片作为二维码的logo,在windows环境下二维码可以正常生成,但是部署到生产测试环境之后二维码生成报错,FileNotF ...