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

传送门: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==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;
}

 

 

posted @ 2018-07-17 21:44  西*风  阅读(267)  评论(0编辑  收藏  举报