首页 > ACM题库 > HDU-杭电 > HDU 1288 Hat’s Tea[解题报告] C++
2013
12-04

HDU 1288 Hat’s Tea[解题报告] C++

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,那么即使满足后面的,也有零头剩余
		{
			cout<<"Hat cannot buy tea."<<endl;
			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
			{
				cout<<"Hat cannot buy tea."<<endl;
				continue;
			}
		}
		else
		{
			if(b>0)
				b--;
			else
			{
				if(a>=5)
					a-=5;
				else
				{
					cout<<"Hat cannot buy tea."<<endl;
					continue;
				}
			}
			n+=5;
			if(n<=10*x10)
			{
				c=n/10;
				printf("%d YiJiao, %d WuJiao, and %d ShiJiao\n",a,b,c);
				continue;
			}
			else
			{
				cout<<"Hat cannot buy tea."<<endl;
				continue;
			}
		}
	}
	return 0;
}


  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.

  2. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。