hdu 1288Hat's Tea

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

分析:主要满足两个条件 1:刚好等于茶价格。2:硬币尽可能的多。
很明显要用贪心解题,试了用了下暴力,果断的超时了。后面贪心写了下,总是wa,实在是不想写了,找了份代码看看,其实发现贪心思路都差不多,但是别人细节方面处理的太好了。我不知道用了多少if(),代码都没法看了。发个解释了的代码吧下。以后再回来看看这题吧!
思路大概是:对于不能过的情况先处理下。然后对5的存在情况讨论。如果存,现把必须要的1个数(n%5)x确定,然后对a1=(n-x)/5即把1尽量换成5.这步可以尽量的贪心5,为什么呢?因为把1的个数去掉了,所以 (n-a1)%10余数要么为0,或为5.如果为5的话,加一个5就能被10 整除了。其他情况就好处理了,直接看代码即可!

#include<iostream>
#include<cstring>
#include<string> 
#include<algorithm>
#include<cmath>
using namespace std;
#define FAIL {puts("Hat cannot buy tea.");continue;}
int n, t1, t2, t3, a1, a2, a3;

int main()
{
    while (cin >> n >> t1 >> t2 >> t3)
    {
        if (!n && !t1 && !t2 && !t3) break;
        if (t1 + 5 * t2 + 10 * t3 < n) FAIL
        if ((t1 == 0) && (t2 == 0) && (n % 10)) FAIL
        if ((t1 == 0) && (n % 5)) FAIL
        a1 = a2 = a3 = 0;
        if (t2) 
        {
            if (t1 < n % 5) FAIL
            a1 = n % 5 + min(n - n % 5, (t1 - n % 5) / 5 * 5);//取n-n%5:表示。取(t1-n%5)/5*5对于5存在, 对一 贪心5。
                                                              //如果5+1能给完价格,不需要管。否则,贪心10,顶多后面减5; 
            n -= a1;
            a2 = (n % 10 == 5) ? 1 : 0;//n%10 为5,一个5是必须的,再把另外的5贪心成10。 
            a2 += min((n - 5 * a2) / 10 * 2, (t2 - a2) / 2 * 2);//(n - 5 * a2) / 10 * 2表示5+1能给完, 
                                                                 //(t2 - a2) / 2 * 2  对5尽量贪心10,即把多余的5转换成10. 
            n -= a2 * 5;
        }
        else
        {
            if (t1 < n % 10) FAIL
            a1 = n % 10 + min(n - n % 10, (t1 - n % 10) / 10 * 10);
            a2 = 0;
            n -= a1;
        }
        a3 = n / 10;
        cout << a1 << " YiJiao, " << a2 << " WuJiao, and " << a3 << " ShiJiao" << endl;
    }
    return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值