HDU 1288(Hat's Tea)

注意必须钱数恰好等于茶钱才能购买,大于茶钱无法购买。

使用两层 for 循环,尽可能用一角硬币,其次用五角硬币,最次用十角硬币。

#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;
}

继续加油。

发布了206 篇原创文章 · 获赞 1 · 访问量 8975

猜你喜欢

转载自blog.csdn.net/Intelligence1028/article/details/104864003