博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Charlie's Change POJ - 1787
阅读量:6369 次
发布时间:2019-06-23

本文共 3360 字,大约阅读时间需要 11 分钟。

Time limit

 

1000 ms
Memory limit 30000 kB

 

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
#include
#include
#include
#include
using namespace std;const int M = 10003;const int INF = 0x3f3f3f3f;map
num, val;int dp[M], used[M], path[M]; //dp[i]表示凑齐价值 i 时所用的最大硬币数量 //used[i]表示在价值 i 时已经用了当前种类硬币数量 //path[i] 下标表示当前价值, 值表示凑成价值 i 之前的总价值,即用了 //某一硬币之前的价值, i - path[i] 则表示这一过程中使用的硬币价值int main(){ int p ,c1 ,c2,c3,c4; while(cin>>p>>c1>>c2>>c3>>c4) { if (p + c1 + c2 + c3 + c4 == 0)break; num[1] = c1; num[2] = c2; num[3] = c3; num[4] = c4; val[1] = 1; val[2] = 5; val[3] = 10; val[4] = 25; //练习一下映射 memset(path, 0, sizeof(path)); //路径初始化 for(int i = 1 ;i <= p ; i ++) { dp[i] = -INF; } dp[0] = 0; //基础部分,将除了起点设置为0外,其余的初始值均为负无穷大, //意味着某一状态只有从起点开始转移,其值才有效 for (int i = 1; i <= 4; i++) { memset(used, 0, sizeof(used)); //初始化各个价值的硬币使用数 for(int j = val[i] ; j<= p ; j++) { if (used[j - val[i]] < num[i] && dp[j-val[i]] >=0 && dp[j - val[i]] + 1 > dp[j]) //硬币数量足够转移,且转移后的硬币数量更多,以及转移的前一个状态有效 { dp[j] = dp[j - val[i]] + 1; //表示从前一个状态转移过来 used[j] = used[j - val[i]] + 1; //表示从 j - val[i] 价值转移而来用了一个硬币 path[j] = j - val[i]; //下标表示当前价值,其值表示转移起点的价值 } } } if(dp[p] < 0) //若dp[i]<0表示这一价值无法凑齐 { cout << "Charlie cannot buy coffee." << endl; continue; } map
ans; ans[1] = 0; ans[5] = 0; ans[10] = 0; ans[25] = 0; //代表各个价值对应的硬币的使用数量 int now = p; //从终点开始回溯 while(true) { if (now == 0)break; //价值为0 ,代表已经走回了起点 ans[now - path[now]]++; //now - path[now] 表示两个状态之间的差值,此差值就是各个硬币的价值,所以对应价值的硬币数量加一 now = path[now]; //为下一次做准备 } printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[1], ans[5], ans[10], ans[25]); } return 0;}

 

转载于:https://www.cnblogs.com/winter-bamboo/p/10634459.html

你可能感兴趣的文章
刷leetcode第704题-二分查找
查看>>
debug_backtrace() 函数生成一个 backtrace(追踪)
查看>>
第七天,还是盒子
查看>>
XAMPP软件包下载
查看>>
XXL-JOB初体验-ORACLE版
查看>>
沉思录:别人的棺材
查看>>
jersey + spring + mybatis + redis项目搭建
查看>>
PAT 1006 部分正确_另一种解法
查看>>
在Keil环境下使用JLink实现printf输出重定向至debug窗口
查看>>
postgres的\d命令不显示全部的用户表
查看>>
poj 3468 A Simple Problem with Integers
查看>>
OOA/OOD/OOP细讲
查看>>
Tomcat 系统架构与设计模式_ 设计模式分析
查看>>
本地串口TCP/IP 映射到远端串口
查看>>
锁机制探究
查看>>
硬盘直接引导启动Manjaro Linux iso
查看>>
CodeSmith代码生成工具介绍
查看>>
几个常用且免费的接口
查看>>
jQuery文件上传插件 Uploadify更改错误提示的弹出框
查看>>
RHEL6下Apache与Tomcat整合
查看>>