传送门: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. Git错误解决(windows版本下的Git Shell)

    第一个问题:怎么也不能将自己本地仓库代码pull到GitHub网站上? git push origin master Warning: Permanently added 'github.com,19 ...

  2. JBPM学习第5篇:Mysql配置

    1.工作台用户Authentication配置 JBPM web工作台预安装了用户认证与授权模块,位于jbpm-console-7.1.0.Final-wildfly-10.1.0.Final.war ...

  3. ES6的新知识点

    一.变量 原有变量: var的缺点: 1.可以重复声明 2.无法限制修改 3.没有块级作用域 新增变量: let :不能重复声明,变量-可以修改,块级作用域 const:不能重复声明,变量-不可以修改 ...

  4. 【转】mouseover和mouseenter的区别

    一.当绑定着两个事件的元素里面没有子元素的时候,这两个事件的触发效果是一致的: x=0; y=0; $(document).ready(function(){ $("div.over&quo ...

  5. Hnoi2004 金属包裹

    传送门 三维凸包模板题……只是听了听计算几何的课之后心血来潮想写的…… 我的做法很无脑是吧……暴力枚举三个点组成的三角形,然后枚举剩下的点,判断其余点是否都在这个三角形的同一侧,是的话则说明这个三角形 ...

  6. Python-网络编程(一)

    首先我们python基础部分已经学完了,而socket是我们基础进阶的课程,也就是说,你自己现在完全可以写一些小程序了,但是前面的学习和练习,我们写的代码都是在自己的电脑上运行的,虽然我们学过了模块引 ...

  7. Python代码 变量None的使用

    代码中经常会有变量是否为None的判断,有三种主要的写法: 第一种是'if x is None': 第二种是 'if not x:': 第三种是'if not x is None'(这句这样理解更清晰 ...

  8. 原生js操作DOM基础-笔记

    原文参考http://mp.weixin.qq.com/s?__biz=MzU3MDA0NTMzMA==&mid=2247485490&idx=1&sn=15197b4b53e ...

  9. ANN神经网络——实现异或XOR (Python实现)

    一.Introduction Perceptron can represent AND,OR,NOT 用初中的线性规划问题理解 异或的里程碑意义 想学的通透,先学历史! 据说在人工神经网络(artif ...

  10. Spring boot配置注意事项

    SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类.这个类的位置很关键: 如果App ...