hdu1288(卖荼)

来源:互联网 发布:淘宝联盟优惠券转换器 编辑:程序博客网 时间:2024/05/03 01:30

题目注意两点:1.要尽可能多的钱的数量   2.一定要满足恰好支付的钱能够卖到荼叶,不能多支付钱!!!

我就是没有看清题的第2个意思,WA了几次!!!!

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 9510 0 0 0

Sample Output
Hat cannot buy tea.3 YiJiao, 115 WuJiao, and 0 ShiJiao
#include<iostream>using namespace std;int main(){    int sum,l,w,s,kl,kw,ks,flog;    while(cin>>sum>>l>>w>>s)    {        if(sum==0&&l==0&&w==0&&s==0)        break;        flog=1;        if(l+5*w+10*s<sum)//总钱数小于荼的单价,卖不到        {            cout<<"Hat cannot buy tea."<<endl;            continue;        }        if(sum<=l)//荼的单价不大于1毛钱的总数        {            kl=sum;kw=ks=0;        }        else//荼的单价大于1毛钱的总数        {            kl=l;  sum-=kl;            kw=sum/5;            if(kw<w)            {                sum-=kw*5;                  if(sum>0)                  {                      kw++;                      sum-=5;                      if(sum+kl<0)                      flog=0;                      else                      kl+=sum;                  }                ks=0;            }            else            {                kw=w; sum-=kw*5;                ks=sum/10;                sum-=ks*10;                if(sum>0&&sum<10)                {                    ks++;                    sum-=10;                    if(sum<=-5)                    {                        if(kw>0)                        {                            kw--;                            sum+=5;                        }                        if(kl+sum<0)                        flog=0;                        else                        kl+=sum;                    }                    else if(sum<0&&sum>-5)                    if(kl+sum<0)                    flog=0;                    else                    kl+=sum;                }            }        }        if(flog)        {        cout<<kl<<" YiJiao, "<< kw<<" WuJiao, and "<<ks<<" ShiJiao"<<endl;        }        else        {            cout<<"Hat cannot buy tea."<<endl;        }    }    return 0;}


原创粉丝点击