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[]的01背包,剩下的就是怎么初始化dp[]的问题,如果不会初始化 请点击传送门

题解如下

#include<iostream>
#include<string.h> using namespace std;
const int Len = 10005;
int dp[Len]; //⚠️ 一定要注意 dp 使用来存生什么的,是用来存储使用各种硬币的总数量
int path[Len][2]; //在存储空背包空间为j时 path[j][0] 存储的的是在dp[j]状态下使用的那种硬币,path[j][1] 为在存储了某种硬币后 所剩余的空间
int coin[4] = {1,5,10,25};
int ans[26]; //存储每种硬币使用的数量
struct Node
{
int num,all_pri,pri; //在进行二进制拆分后得到的 得到的一种新的硬币 这种新硬币消耗原来的 num个硬币,新硬币的价值是 all_pri,原来硬币的价值是 pri
}a[Len]; //存储记录转移情况 int main()
{
int cnt[4];
int cost;
while(scanf("%d%d%d%d%d",&cost,&cnt[0],&cnt[1],&cnt[2],&cnt[3]) && (cost + cnt[0] + cnt[1] + cnt[2] + cnt[3]))
{
int pos = 0; //二进制拆分后物品变成 pos 件
for(int t = 0;t < 4;t ++)
{
int count = cnt[t];
//这里就是进行 二进制 拆分
for(int k = 1;k <= count && k * coin[t] <= cost;k *= 2)
{
count -= k;
a[pos].num = k;
a[pos].all_pri = k * coin[t];
a[pos ++].pri = coin[t];
}
if(count) //二进制拆分的最后一个判读
{
a[pos].num = count;
a[pos].all_pri = count * coin[t];
a[pos ++].pri = coin[t];
}
} memset(dp,0,sizeof(dp));
dp[0] = 1; //⚠️这里恰好装满、使用完 情况初始化dp的技巧
memset(path,0,sizeof(path)); for(int i = 0;i < pos;i ++)
{
for(int j = cost;j >= a[i].all_pri;j --)
{
if(dp[j - a[i].all_pri] > 0 && dp[j - a[i].all_pri] + a[i].num > dp[j])
{
dp[j] = dp[j - a[i].all_pri] + a[i].num; //在
path[j][0] = i; //记录在 dp[j] 达到最大使用硬币使用数量时,消耗的是哪种硬币
path[j][1] = j - a[i].all_pri; //dp[j]达到最大使用硬币数量时背包中剩余的空间 (这里的操作时 是为了 方便回推 )
}
}
}
if(!dp[cost])
{
printf("Charlie cannot buy coffee.\n");
}
else
{
memset(ans,0,sizeof(ans));
int cost_ = cost;
while(cost_)
{
int i = path[cost_][0]; // 在背包空间为j的情况时(dp[j])达到最优情况使用了 第i件物品
ans[a[i].pri] += a[i].num; //使用第i种硬币时的 消耗 组成i硬币的某种硬币的数量
cost_ = path[cost_][1]; //这一句就是要 往状态转移方程的上一个 状态回退,因为选则了 i 这种硬币,所以背包剩余的空间为 path[cost_][1]
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[1],ans[5],ans[10],ans[25]);
}
} return 0;
}

B - Charlie's Change的更多相关文章

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

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

  2. Charlie's Change(完全背包+路径记忆)

    Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 D ...

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

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

  4. Charlie's Change POJ - 1787

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

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

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

  6. poj1787 Charlie's Change

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

  7. [POJ 1787]Charlie's Change (动态规划)

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

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

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

  9. poj 1787 Charlie's Change

    // 题意 给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1<=p&l ...

随机推荐

  1. Tomcat 之startup.bat启动失败案例

    今天我在部署一个Tomcat环境时,各种变量都配置完了,最后启动Tomcat时,Tomcat一闪而过,当时我的内心是崩溃的~~ 然后我就开始百度.定位问题.进入cmd命令行窗口,cd进入到Tomcat ...

  2. mysql的锁与事务

    1. MySQL中的事物 1.InnoDB事务原理 1. 事务(Transaction)是数据库区别于文件系统的重要特性之一,事务会把数据库从一种一致性状态转换为另一种一致性状态. 2. 在数据库提交 ...

  3. json中存整形数值,前端返回为空

    实现目标: 在servlet中将整型数值转为json,然后在前端获取 问题描述: 前端获取为空,显示为:console.log打印为:{} 解决办法: 在servlet中将数值转为json格式再进行传 ...

  4. JavaScript(js)函数声明与函数表达式的区别

    在JavaScript中,函数是经常用到的,在实际开发的时候,我想很多人都没有太在意函数的声明与函数表达式的区别,但是呢,这种细节的东西对于学好js是非常重要的. 函数声明与函数表达式用代码写出来是这 ...

  5. CVPR 2020 全部论文 分类汇总和打包下载

    CVPR 2020 共收录 1470篇文章,根据当前的公布情况,人工智能学社整理了以下约100篇,分享给读者. 代码开源情况:详见每篇注释,当前共15篇开源.(持续更新中,可关注了解). 算法主要领域 ...

  6. 编译 ijg JPEG V8 库 GIF 库

    libjpeg-turbo-1.2.1太老了,不支持,从内存解压,这里编译支持 jpeg_mem_src 的 JPEG V9 wget http://www.ijg.org/files/jpegsrc ...

  7. 《前端之路》 - 初试 TypeScript(一)基础数据类型

    一.先讲讲 TypeScript 什么是 typeScript ? typeScript 是 Javascript 的超集 我们用一张图来简单介绍下 ts 和 js 清清楚楚明明白白的关系- 为什么会 ...

  8. Java 创建、编辑、删除Excel命名区域

    Excel命名区域,即对指定单元格区域进行命名,以便对单元格区域引用,如在公式运用中可以引用指定命名区域进行公式操作.在创建命名区域时,可针对整个工作簿来创建,即workbook.getNameRan ...

  9. Python输出水仙花数,用逗号分隔

    描述 "水仙花数"是指一个三位整数,其各位数字的3次方和等于该数本身.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪ ...

  10. Asp.Net Core 中IdentityServer4 授权原理及刷新Token的应用

    一.前言 上面分享了IdentityServer4 两篇系列文章,核心主题主要是密码授权模式及自定义授权模式,但是仅仅是分享了这两种模式的使用,这篇文章进一步来分享IdentityServer4的授权 ...