HDU 1288(Hat's Tea)

Note that you must exactly equal to the amount of money money to buy tea, tea is greater than the money can not buy.

Using two layers for recycling, as far as possible with a dime, followed by Pentagon coins, most times with ten horns coins.

#include <iostream>
using namespace std;

int main()
{
	int price; //茶钱
	int one, five, ten; //一角、五角、十角硬币最大数量
	while (cin >> price >> one >> five >> ten)
	{
		if (price == 0 && one == 0 && five == 0 && ten == 0)
			break;
		if (one + five * 5 + ten * 10 < price) //全部加起来也不够
		{
			cout << "Hat cannot buy tea." << endl;
			continue;
		}

		bool flag = false;
		int i, j; //十角硬币数量,五角硬币数量
		for (i = 0; i <= ten; i++) //最次用十角硬币
		{
			for (j = 0; j <= five; j++) //其次用五角硬币
			{
				if (price - i * 10 - j * 5 <= one) //尽可能用一角硬币
				{
					flag = true;
					break;
				}
			}
			if (flag)
				break;
		}
		one = price - i * 10 - j * 5; //一角硬币数量
		if (one >= 0)
			cout << one << " YiJiao, " << j << " WuJiao, and " << i << " ShiJiao" << endl;
		else //钱数超过茶钱
			cout << "Hat cannot buy tea." << endl;
	}
	return 0;
}

Keep up.

Published 206 original articles · won praise 1 · views 8975

Guess you like

Origin blog.csdn.net/Intelligence1028/article/details/104864003