Hat's Tea
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1139 Accepted Submission(s): 238
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
题目大意:话说去买茶,需要正好的钱才行,另外花的钱的个数最多
其实是一个简单的贪心问题,小的多取一点,多的少取一点,别人好多正着求的,我是倒着写的,感觉简单,特来分享代码:
AC代码:
1 #include<algorithm> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<map> 6 #include<queue> 7 #include<iostream> 8 using namespace std; 9 int main() 10 { 11 int i,j,k,t,m,x,y,z,n; 12 while(cin>>m>>x>>y>>z && m+x+y+z) 13 { int sum=0; 14 sum=x+y*5+z*10; 15 n=m; 16 if(sum<m) 17 cout<<"Hat cannot buy tea."<<endl; 18 else 19 { 20 m=sum-m; 21 int z1=m/10; 22 if(z1<z) m=m-z1*10,z=z-z1; 23 else m=m-z*10,z=0; 24 int y1=m/5; 25 if(y1<y) m=m-y1*5,y=y-y1; 26 else m=m-y*5,y=0; 27 int x1=m; 28 if(x1<x) m=m-x1,x=x-x1; 29 else m=m-x,x=0; 30 if(m!=0) 31 printf("Hat cannot buy tea. "); 32 else printf("%d YiJiao, %d WuJiao, and %d ShiJiao ",x,y,z); 33 } 34 } 35 return 0; 36 }