硬币面值问题

The currency system in Colorlandconsists of various types of cins. The coin denominations followthese simple rules:

  • The denominations are distinctpositive integers.

  • There is a coin type withdenomination 1.

  • For each pair of different coin types, the denomination ofone coin type divides the denomination of the other one.

You are given a int[ ] values containing all the availabledenominations in ascending order.


Coins of differentdenominations look exactly the same except that theyhave different colors. Each coin in Colorland has exactly onecolor. The coin colors follow these even simpler rules:

  • All coins of the same type are ofthe same color.

  • No two coins of different types are of the same color.

You know all coin denominations used in Colorland, but youdon't know their colors.You don't even know the set of colorsused on the coins.



For each denomination, you'd liketo know the color of coins of this denomination. To accomplish this,you've got a credit card with an infinite amount of money. You canperform a single query to an ATM which can also provide you with aninfinite amount of money. The query is described by a positiveinteger X, which means that you want to receive exactly X units ofmoney from the ATM. The ATM will provide you with the requestedamount.You also know that the requested amount will be paid usingthe smallest possible number of coins. (Note that this rulealways uniquely determines the set of coins chosen to make thepayment.)



Return "Possible" (quotes forclarity) if it's possible to determine the color of coins of eachdenomination, and return "Impossible" otherwise.



解析:任意两种不同硬币的币值是成倍数关系的,考虑面值相邻两种硬币a,b( b > a ),如果k = b / a ,那么在最终的硬币中,a出现的次数一定小于k,如果大于或者等于k,那么则可使用b来代替,会得到更少数目的硬币。

例如:现在有两种面值1 3那么最终的结果中1出现的次数一定小于3(可以是12,不能是0,每个硬币都必须出现),如果1出现的次数大于等于3,那可以使用3,得到更少的硬币数。



Answer:

string isPossible(vector <int> values)
{
	vector<int> count;//记录相邻硬币之间的倍数,也就是记录对应的硬币最多出现的次数
	for(int i = 0; i < values.size()-1 ; ++i)
	{
		count.push_back(values[i+1]/values[i]);
	}
	sort(count.begin(), count.end());

	for(int i = 0 ; i < count.size() ; ++i )
	{
		if (count[i] < (i+2))
		{
			return string("Impossible");
		}
	}
return string("Possible") ;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值