描述
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.

输入
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.输出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.".
 
样例输入

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

样例输出

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.

 
对于这题说法不一, 有人说是多重背包, 有人说是完全背包, 反正我是当多重背包看了, 但是看看人家写的完全背包还是不难理解的, 挺巧妙的
 
自我感觉第二种解法更方便一些
 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f; #define met(a,b) (memset(a,b,sizeof(a)))
#define N 11000
#define INF 0x3f3f3f3f int pre[N], v[]={,,,,}, dp[N];
int used[N]; /** dp[i] 代表组成i元时需要的最多的硬币
used[i] 代表第i种硬币用了几次(感觉这点很好,能将多重背包转化为完全背包)
pre[j] 记录的是j从哪个状态转化过来的 */ int main()
{
int p, num[]={}; while(scanf("%d%d%d%d%d", &p, &num[], &num[], &num[], &num[]), p+num[]+num[]+num[]+num[])
{
int i, j, ans[]={}; met(dp, -);
met(pre, -);
dp[] = ;
for(i=; i<=; i++)
{
memset(used, , sizeof(used));
for(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] = dp[j-v[i]]+;
used[j] = used[j-v[i]]+;
pre[j] = j-v[i];
}
}
} if(dp[p]<)
printf("Charlie cannot buy coffee.\n");
else
{
met(ans, );
i = p;
while()
{
if(pre[i]==-) break;
ans[i-pre[i]]++;
i = pre[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[v[]], ans[v[]], ans[v[]], ans[v[]]);
} }
return ;
}

另一种解法:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm> using namespace std;
#define met(a,b) (memset(a,b,sizeof(a)))
const int N=;
int dp[N], v[]={,,,,};
int num[N][];
//dp[j]为咖啡的价格为j时,所能花费的最多钱币数
//num[j][i],表示咖啡的价格为j时,花费的第i种货币的个数
int main()
{
int a[], p; while(scanf("%d%d%d%d%d", &p, &a[], &a[], &a[], &a[]), p+a[]+a[]+a[]+a[])
{
int i, j, k; met(dp, -);
met(num, ); dp[] = ;
for(i=; i<=; i++)
{
for(j=v[i]; j<=p; j++)
{
if(dp[j-v[i]]>= && dp[j-v[i]]+>dp[j] && num[j-v[i]][i]<a[i])
{
dp[j] = dp[j-v[i]] + ;
for(k=; k<=; k++)
{
if(k==i)
num[j][k] = num[j-v[i]][k]+;
else
num[j][k] = num[j-v[i]][k];
}
}
}
} if(dp[p]==-)
printf("Charlie cannot buy coffee.\n");
else
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", num[p][], num[p][], num[p][], num[p][]);
}
return ;
}

(多重背包+记录路径)Charlie's Change (poj 1787)的更多相关文章

  1. Charlie's Change POJ - 1787

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

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

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

  3. 01背包记录路径 (例题 L3-001 凑零钱 (30分))

    题意: 就是找出来一个字典序最小的硬币集合,且这个硬币集合里面所有硬币的值的和等于题目中的M 题解: 01背包加一下记录路径,如果1硬币不止一个,那我们也不采用多重背包的方式,把每一个1硬币当成一个独 ...

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

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

  5. poj 1787 背包+记录路径

    http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Subm ...

  6. 完全背包记录路径poj1787 好题

    这题有点多重背包的感觉,但还是用完全背包解决,dp[j]表示凑到j元钱时的最大硬币数,pre[j]是前驱,used[j]是凑到j时第i种硬币的用量 △回溯答案时i-pre[i]就是硬币价值 #incl ...

  7. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  8. 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...

  9. UVA 624(01背包记录路径)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. JS正则2

    正则表达式可以:•测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证•替换文本.可以在文档中使用一个正则表达式来标 ...

  2. tcp/ip http socket笔记

    1.TCP/IP协议是传输层协议,主要解决数据如何在网络中传输 HTTP是应用层协议,主要解决如何包装数据 2.TCP连接的三次握手 第一次握手:客户端发送syn包到服务器,并进入SYN_SEND状态 ...

  3. 点击更多button显示更多数据的功能实现思路代码

    此功能是根据自己思路去慢慢做出来的,做的不够专业,希望有懂这个的前辈给自己指点指点. //分界线———————————————————————————————————————————————————— ...

  4. man curl_easy_perform(原创)

    curl_easy_perform(3)           libcurl 手册                  curl_easy_perform(3) 名字 curl_easy_perform ...

  5. hdu 1872(看病要排队)(优先队列)

    看病要排队 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. was部分更新

    在WAS中,应用的配置是从config/cells....目录下读取:而资源从/installedApps目录下读取 故当配置文件(例web.xml)发生改变时,只更新应用程序资源文件/install ...

  7. JSON 的应用

    使用  JSON 需要的 jar 包:

  8. [刘阳Java]_MyBatis_映射文件的select标签入门_第3讲

    1.Mybatis映射文件的<select>标签主要帮助我们完成SQL语句查询功能,<select>标签它包含了很多属性,下面简单对<select>标签的属性做一个 ...

  9. 利用Kinect将投影变得可直接用手操控

    Finally 总算是到了这一天了!假期里算法想不出来,或者被BUG折磨得死去活来的时候,总是YY着什么时候能心情愉快地坐在电脑前写一篇项目总结,今天总算是抽出时间来总结一下这神奇的几个月. 现在回过 ...

  10. IEEE二进制浮点数算术标准学习

    看到有网上有个项目是要求将浮点数用二进制表示出来,需要用IEEE754标准,查了查维基和深入理解计算机系统,重新学习了一遍浮点数在计算机中的表示和内存中的存储, 先简单的做个笔记,后面需要更深入的理解 ...