hdu 1288 Hat's Tea

来源:互联网 发布:选择财物软件方案 编辑:程序博客网 时间:2024/05/18 22:12

Hat's Tea

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1492    Accepted Submission(s): 319


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
 
题目大意:
题目大意呢就是说Hat经常去 自动售茶机去买茶,那么问题来了:
首先自动售茶机只收硬币;
然后必须是和其价格等价的硬币才能够拿到茶,大于小于都是不可以的;
最后还要求,要多给的硬币中最多数量的硬币才能拿到茶;(就是求最多用多少个硬币能买到茶;)

给出AC代码和思路:
/*先说下题目大意和要求吧,就是要求用已给的 1,5,10,的硬币去凑出自动售茶机所给的标价,并且要求是竟可能多的硬币;这题刚开始使用暴力直接枚举然后遍历找到最大的,思想很简单,但是效率确实不行 提交  超时;然后我就考虑到要用贪心:首先贪最小的 1 的,然后 贪 5 的 然后贪 10 的;但是这中间也遇到了一些问题,比如,如果所给的硬币中是没有面值为 1的,或者没有 5 的,或者没有 10 的;(没有 1,5或1,10或5,10的很好处理应为他只剩一种了),那么我们的贪心顺序就会出现问题,所以我们需要跳过一些不要贪心的;那么我的贪策略就是,每一次考虑拿最多的,当让是越小的面值的需要的越多;*/#include<iostream>using namespace std;int main(){int va, yi, wu, shi;int  x, y, z, sum;while (cin >> va >> yi >> wu >> shi){if (va == 0 && yi == 0 && wu == 0 && shi == 0)break;bool flag = false;if (va > yi + wu * 5 + shi * 10)printf("Hat cannot buy tea.\n");else if (yi >= va)printf("%d YiJiao, 0 WuJiao, and 0 ShiJiao\n", va);else{int temp = va;for (int i = 0; i <= yi; i++){//int t_wu = temp / 5;if (temp % 5 == 0){flag = true; break;}else temp = temp - 1;}if (flag){sum = va - yi;x = yi;while (sum % 5 != 0 && x >= 0){x--;sum++;}if (wu > 0){y = sum / 5;if (y > wu){y = wu;sum = sum - y * 5;}else sum = sum - y * 5;if (sum == 0){z = 0;}else{while (sum % 10 != 0 && y >= 0){y--;sum += 5;}z = sum / 10;}printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n", x, y, z);}else//如果不存在面值为 5 的硬币,直接跳过不考虑;{y = 0;while (sum % 10 != 0 && x >= 0){x--;sum++;}if (sum % 10 == 0 && sum - 10 * (sum / 10) == 0){z = sum / 10;printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n", x, y, z);}elseprintf("Hat cannot buy tea.\n");}}elseprintf("Hat cannot buy tea.\n");}}return 0;}


1 0