Hat's Tea
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1332 Accepted Submission(s): 288
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.
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
注:无语+崩溃,各种WA,需要考虑的情况比较多,思绪有点乱。。。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a,b,c;
int x1,x5,x10,t,m;
int min(int a,int b){
return a>b?b:a;
}
int main(){
while( ~scanf("%d%d%d%d",&n,&a,&b,&c) ){
if( !n && !a && !b && !c ) break;
if( n > a+5*b+10*c ){ //总额少
printf("Hat cannot buy tea.\n");
continue;
}
if( !a && !b && ( n % 10 ) ){ //只有10角的,且总价不是10的倍数
printf("Hat cannot buy tea.\n");
continue;
}
if( !a && ( n % 5 ) ){ //没有1角的,且总价不是5的倍数
printf("Hat cannot buy tea.\n");
continue;
}
x1 = x5 = x10 = 0;
if( b ){ //有5角的
t = n % 5;
if( a < n % 5 ){ //1角的个数不够补足总价余5剩下的零角
printf("Hat cannot buy tea.\n");
continue;
}
x1 = t + min( n-n%5 , (a-t)/5*5 );
n -= x1;
x5 = ( n % 10 == 5 ) ? 1 : 0;
x5 += min( (n-5*x5)/10*2 , (b-x5)/2*2 );
n -= x5 * 5;
}
else{ //没有5角的
t = n % 10;
if( a < n % 10 ){ //1角的个数不够补足总价余10剩下的零角
printf("Hat cannot buy tea.\n");
continue;
}
x1 = t + min( n-n%10 , (a-t)/10*10 );
n -= x1;
}
x10 = min( n/10 , c );
printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",x1,x5,x10);
}
return 0;
}