
B - Charlie's Change dp背包路径
发布日期:2021-05-07 08:56:54
浏览次数:29
分类:技术文章
本文共 2549 字,大约阅读时间需要 8 分钟。
原题:
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.题意:有不同数量的1,5,10,25币值的硬币,用最多的硬币凑成P(所求值)。 思路:很明显这是一道多重背包的问题,但是如果我们只是在求最优解的过程中,记录每种硬币出现的个数,就会出现问题。所以我们要记录每个硬币从哪里来,就是记录路径。此外这个多重背包可以转换为完全背包求解会更容易。 代码如下:
#include#include #include #include using namespace std;int mon[5][2];int num[10005];int temp[10005];int dp[10005];int sum[30];int main(){ int n; mon[0][1]=1,mon[1][1]=5,mon[2][1]=10,mon[3][1]=25; //硬币价值 while(cin>>n>>mon[0][0]>>mon[1][0]>>mon[2][0]>>mon[3][0]) //硬币数 { if(n==0&&mon[0][0]==0&&mon[1][0]==0&&mon[2][0]==0&&mon[3][0]==0) return 0; memset(dp,-1,sizeof(dp)); //dp赋值为-1是为了判断是否会出现无法没有足够的零钱的情况 memset(temp,0,sizeof(temp)); dp[0]=0; temp[0]=-1; //起点 for(int i=0; i<4; ++i) { memset(num,0,sizeof(num)); for(int j=mon[i][1]; j<=n; ++j) { //num[j-mon[i][1]] =0&&dp[j]<=dp[j-mon[i][1]]&&num[j-mon[i][1]]
发表评论
最新留言
很好
[***.229.124.182]2025年03月29日 04时03分22秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
怎么去利用已有的数据做分析?
2019-03-04
专升本——英语视频学习
2019-03-04
Future education software
2019-03-04
不知道考研各科课程那个老师的课好?怎么选择安排?在哪找课程课本资料?看这里
2019-03-04
考研高数一_高数二_高数三考试大纲
2019-03-04
【考研高数-高等数学-基础】第四章 不定积分
2019-03-04
【考研英语-基础-简单句】简单句的核心变化_谓语情态
2019-03-04
17级软件技术二班刘鑫磊安卓学期总结
2019-03-04
配置Log4j详细版
2019-03-04
数据结构 第五章 二叉树-1
2019-03-04
[Easy] 100. Same Tree
2019-03-04
PVE+集客AC+K2T-AP
2019-03-04
Jetson AGX Xavier硬件自启动
2019-03-04
网页实时显示已经运行了多少天 html+js
2019-03-04
判断移动端(手机)还是客户端(电脑)打开网页并跳转不同页面(首页)
2019-03-04
眼睛跟随鼠标转动的小黄人 html+css+js
2019-03-04
平均年龄,,数字求和
2019-03-04
简单的字符串操作(注意要点)
2019-03-04
统计字符数
2019-03-04