传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1288

Hat's Tea

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2127    Accepted Submission(s): 484

Problem Description
Hat is a member of PG Studio. Hat codes a lot and so he often buys tea at tea vending machine. But the tea vending machine just eat coins and spit out tea, if you feed the machine more coins than the tea’s price and the machine will not spit out your change.
Your program will be given numbers and types of coins Hat has and the tea price. The tea vending machine accepts coins of values 1, 5, 10 RMB (Jiao). The program should output which coins Hat has to use paying the tea so that he uses as many coins as possible.
 
Input
Each line of the input contains four integer numbers separated by a single space describing one situation to solve. The first integer on the line N, , is the tea price in Jiao. Next four integers , , are the numbers of YiJiao (1 Jiao.RMB), WuJiao (5 Jiao.RMB), and ShiJiao (10 Jiao.RMB) in Hat's valet. The last line of the input contains four zeros and no output should be generated for it.
 
Output
For each situation, your program should output one line containing the string " T1 YiJiao, T2 WuJiao, and T3 ShiJiao ", where T1, T2, T3 are the numbers of coins of appropriate values Hat should use to pay the tea while using as many coins as possible. If Hat does not have enough coins to pay the tea exactly, your program should output "Hat cannot buy tea.”.
 
Sample Input
6653 226 72 352
578 5 127 951
0 0 0 0
 
Sample Output
Hat cannot buy tea.
3 YiJiao, 115 WuJiao, and 0 ShiJiao
 
Author
戴帽子的
 
Source
 
分析:
题目大意呢就是说Hat经常去 自动售茶机去买茶,那么问题来了:

首先自动售茶机只收硬币;
然后必须是和其价格等价的硬币才能够拿到茶,大于小于都是不可以的;
最后还要求,要多给的硬币中最多数量的硬币才能拿到茶;(就是求最多用多少个硬币能买到茶;)
 有两种方法
第一种是正向的贪心:
 

首先判断一些肯定不可能的条件

然后贪心一角硬币,全部使用一角硬币。如果剩下的硬币不是5的倍数。减少一角的使用,使剩下的硬币成为5的倍数

然后贪心五角硬币,如果剩下的硬币不是10的倍数,减少一个五角的使用,如果五角的使用个数为0,减少5个一角的使用个数

如果没有5个一角的,则不满足

这种方法,实现起来比较困难,虽然思想简单,但是要注意的细节太多了

我没有写出来。。。

第二种方法:反向的贪心

1角数量*1+5角数量*5+10角数量*10=X

X-茶价格=y

要你求的是用尽可能多的硬币组成茶价格的值

那么我用尽可能少的硬币组成y

那么剩下的硬币组成的就是价格呀,且硬币的数量还是最大的(因为硬币总数量是确定的)

哈哈哈哈,太聪明了

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int v,num1,num5,num10;
while(~scanf("%d %d %d %d",&v,&num1,&num5,&num10))
{
if(v+num1+num5+num10==)
break; int c1=,c5=,c10=; if(num1+num5*+num10*<v)//所有钱加起来都小于价格
{
printf("Hat cannot buy tea.\n");
continue;
} if(num1>=v)//1角的钱就可以满足价格
{
c1=v;
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",c1,c5,c10);
continue;
} //反着贪心
//总钱减去价格这个值 用到的钱个数尽可能少 等价于 价格用到的钱个数尽可能多
int sum=num1+num5*+num10*-v; //每次都选择面值最大的,这样钱的个数就最少
int x=sum/;
if(x>num10)
{
sum=sum-*num10;
x=;
}else
{
sum=sum-*x;
x=num10-x;
} int y=sum/;
if(y>num5)
{
sum=sum-*num5;
y=;
}else
{
sum=sum-y*;
y=num5-y;
} int flag=; int z=sum;
if(z>num1)//总钱还小于价格,买不了
{
flag=;
}else
{
sum=sum-z;
z=num1-z;
}
if(flag==)
{
printf("Hat cannot buy tea.\n");
}else
{
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",z,y,x);
}
}
return ;
}

HUD 1288 Hat's Tea(反向的贪心,非常好的一道题)的更多相关文章

  1. hdoj 1288 Hat's Tea

    Hat's Tea Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  2. hdu 1288 Hat's Tea

    这个要慢慢理解…… ;}

  3. hdu Hat's Tea

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1288 去买茶,需要正好的钱才行,另外花的钱的个数最多  其实是一个简单的贪心问题,小的多取一点,多的少 ...

  4. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  5. 2018年暑假ACM个人训练题7 题解报告

    A:HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用) B:(还需要研究一下.....) C:HDU 1071 The area(求三个点确定的抛物线的面积, ...

  6. 关于 Softmax 回归的反向传播求导数过程

    对于 \(Softmax\) 回归的正向传播非常简单,就是对于一个输入 \(X\) 对每一个输入标量 \(x_i\) 进行加权求和得到 \(Z\) 然后对其做概率归一化. Softmax 示意图 下面 ...

  7. 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分/LCT+贪心

    Description 给你一个有n个点的树,每个点的包括一个位运算opt和一个权值x,位运算有&,l,^三种,分别用1,2,3表示. 每次询问包含三个数x,y,z,初始选定一个数v.然后v依 ...

  8. CH 5105 Cookies(贪心+DP)

    \(CH 5105 Cookies\) \(solution:\) 真是好题一道!这道题我想了很久很久,就得这一题可以直接完全贪心,可惜最后还是失败了,但是对贪心的深入思考也换来了一个最优解方案.然后 ...

  9. 清北学堂—2020.1提高储备营—Day 1 afternoon(二分、分治、贪心)

    qbxt Day 1 afternoon --2020.1.17 济南 主讲:李佳实 目录一览 1.二分法 2.分治 3.贪心 总知识点:基础算法 一.二分法 (1)算法分析:二分法是一种暴力枚举的优 ...

随机推荐

  1. Java中的断言 Assert

    今天正好遇到了,就记一下 一.作用: 用与编写单元测试 二.assert 关键字 assert 理论上和 if类似, 但是assert 仅仅用于测试, 不能用于业务 如果发现断言无效, 则可能时ide ...

  2. 版本控制器之SVN

    1.开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目中需要一个很复杂的功 ...

  3. js中random的应用

    1.生成一个随机数 var r = Math.random(); console.info(r); 结果生成一个0-1的随机数(返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1)) 2.生成 ...

  4. 移动端适配(3)——rem适配

    rem适配 <meta name="viewport"  content="width=device-width,user-scalable=no"/&g ...

  5. HTML5 localstorage和session操作

    setStorage={ getLocal : function(key){ //获得localStorage里面的值 var storage = window.localStorage; if(st ...

  6. QQ 聊天机器人小薇发布!

    简介 XiaoV(小薇)是一个用 Java 写的 QQ 聊天机器人 Web 服务,可以用于社群互动: 监听多个 QQ 群消息,发现有"感兴趣"的内容时通过图灵机器人进行智能回复 监 ...

  7. 《一马当先 O2O创业真人秀》阿里云创客+项目提交报名中

    传统行业与互联网的相互融合,线上与线下的互通,正在掀起一股“互联网+”新风潮和创业热潮.支付宝钱包.快的打车.淘点点……这些耳熟能详的应用早已成为人们生活的一部分.而越来越多的“互联网+”创新项目,将 ...

  8. <meta name="renderer" content="webkit">

    <meta name="renderer" content="webkit"> 当前国内的大部分主流浏览器(如360)基本都是双核浏览器,所谓双核即 ...

  9. c++实现对输入数组进行快速排序

    #include "stdafx.h" #include <iostream> #include <string> #include <vector& ...

  10. SQL Server ->> Sparse File(稀疏文件)

    Sparse File(稀疏文件)不是SQL Server的特性.它属于Windows的NTFS文件系统的一个特性.如果某个大文件中的数据包含着大量“0数据”(这个应该从二进制上看),这样的文件就可以 ...