HUD 1288 Hat's Tea(反向的贪心,非常好的一道题)

news/2024/7/24 12:49:16

传送门: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
杭电ACM集训队训练赛(VII)
 
分析:
题目大意呢就是说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==0)
            break;

        int c1=0,c5=0,c10=0;
        
        
        if(num1+num5*5+num10*10<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*5+num10*10-v;

        //每次都选择面值最大的,这样钱的个数就最少
        int x=sum/10;
        if(x>num10)
        {
            sum=sum-10*num10;
             x=0;
        }else
        {
            sum=sum-10*x;
            x=num10-x;
        }

        int y=sum/5;
        if(y>num5)
        {
            sum=sum-5*num5;
            y=0;
        }else
        {
            sum=sum-y*5;
            y=num5-y;
        }

        int flag=1;

        int z=sum;
        if(z>num1)//总钱还小于价格,买不了
        {
            flag=0;
        }else
        {
            sum=sum-z;
            z=num1-z;
        }
        if(flag==0)
        {
            printf("Hat cannot buy tea.\n");
        }else
        {
            printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",z,y,x);
        }
    }
    return 0;
}

 

 


http://www.niftyadmin.cn/n/871039.html

相关文章

Python 3.x变量作用域

变量作用域 case2:局部变量与全局变量同名 x"i am global var" def fun():x100print(x) fun() print(x) ***********output************ 100 i am global var >>> 即便同名&#xff0c;函数内访问的变量就是函数内定义的局部var&#xff0c;函数外访问的就…

Linux下大写的LS命令

刚刚一不小心开了大写&#xff0c;且手比脑快&#xff0c;本来想看一下当前目录下有什么文件&#xff0c;结果一辆小火车跑出来了&#xff01;

Java-关键字和标识符

关键字&#xff1a;Java语言中已经被赋予特殊意义的单词&#xff0c;关键字都是小写的英语单词。不能把关键字作为标识符使用。 Java中常见的一部分关键字&#xff1a; publicstaticintbyteifnewreturnelsefortrycasefinalbreakprivatecatchfinallynativethrowsdofinallylongs…

HDU 1286 找新朋友 (欧拉公式或者标记法(其实就是欧拉公式的思想))

传送门&#xff1a; http://acm.hdu.edu.cn/showproblem.php?pid1286 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14909 Accepted Submission(s): 7936 Problem Description新年快到了&#xff0…

Python必知必会内建函数

Python必知必会内建函数 Python提供了大量的内置函数共开发者使用&#xff0c;无需我们再自己写某些常用的功能函数&#xff0c;站在前人肩上&#xff0c;避免重复造轮子&#xff0c;提高开发效率。 当你想做一件事&#xff0c;可以先想一下Python是否提供了内置函数。 常用内…

Java-常量与变量

常量&#xff1a;是指Java程序运行过程中一直不会改变的量称为常量&#xff08;constant&#xff09;。 常量在整个程序中只能被赋值一次。 常量的分类 类型含义数据举例整数常量所有的整数1、123、0、-56、768小数常量所有的小数0.1、-0.5、3.14字符串常量双引号引起来&…

Java基础入门(五)

第五章 Java中的常用类 5.1 String类与StringBuffer类 都位于Java.lang包中&#xff0c;不需要导包就可以直接使用 5.1.1 String类的初始化 使用字符串常量直接初始化一个String对象 String 变量名 字符串; 使用String的构造方法初始化字符串对象 String 变量名 new Stri…

Wait 30 seconds for next check

背景 在OpenPCDet中测试使用–eval_all的时候&#xff0c;出现Wait 30 seconds for next check这样一个dead loop。GitHub有相关Issue&#xff0c;说是不要用–eval_all&#xff0c;直接使用–ckpt来指定epoch。但是我就是想用–eval_all来测试最后30个epochs。## 解决方法 解…