2013
12-04

Hat’s Tea

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.

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.

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.”.

6653 226 72 352
578 5 127 951
0 0 0 0 

Hat cannot buy tea.
3 YiJiao, 115 WuJiao, and 0 ShiJiao

#include <iostream>
using namespace std;
int main()
{
int n,x1,x5,x10;
int a,b,c;
while(cin>>n>>x1>>x5>>x10&&(n+x1+x5+x10))
{
a=b=c=0;
if(n%5>x1)  如果n%5大于x1，那么即使满足后面的，也有零头剩余
{
continue;
}
a=n%5;
n-=a;
x1-=a;
if(n<=x1)
{
a=n+a;
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",a,b,c);
continue;
}
x1/=5;
n-=5*x1;
a+=5*x1;
if(n<=5*x5)
{
b+=n/5;
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",a,b,c);
continue;
}
n-=5*x5;
b=x5;
if(n%10==0)
{
if(n<=10*x10){
c+=n/10;
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",a,b,c);
continue;
}
else
{
continue;
}
}
else
{
if(b>0)
b--;
else
{
if(a>=5)
a-=5;
else
{
continue;
}
}
n+=5;
if(n<=10*x10)
{
c=n/10;
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",a,b,c);
continue;
}
else
{
}