问题描述
有多少种方式,你可以用 1 美元、2 美元和 5 美元的面额支付 N 美元,这样
我尝试过类似的方法,但无法了解如何检查约束
private static void numberOfWays(int number) {
int one = 0,two = 0;
int five = (number - 4) / 5;
if (((number - 5 * five) % 2) == 0)
{
one = 2;
}
else
{
one = 1;
}
two = (number - 5 * five - one) / 2;
System.out.println (one + two + five);
System.out.println (five);
System.out.println (two);
System.out.println (one);}
解决方法
约束很有趣。
“每个面额至少应有一枚硬币”意味着 N < 7
没有解决方案。
“1 美元硬币的数量总是大于 2 美元硬币的数量,2 美元硬币的数量总是大于 5 美元硬币的数量”意味着 numFives = 1
、numTwos = 2
和numOnes = 3
开始,因此 N < 12
没有解决方案。
这满足两个约束。
知道了,你打算怎么攻击它?蛮力?
这听起来像是 knapsack problem 的变体。也许你可以阅读一下。
我认为 simplex method 和带有约束的动态规划是您最好的选择。
,你可以尝试这样的事情:
IntStream.rangeClosed(1,100).forEach(target -> {
final int max1 = (target + 1 - 1) / 1;
final int max2 = (target + 2 - 1) / 2;
final int max5 = (target + 5 - 1) / 5;
IntStream .rangeClosed( 1,max5).forEach(count5 -> {
IntStream .rangeClosed(count5 + 1,max2).forEach(count2 -> {
IntStream.rangeClosed(count2 + 1,max1).forEach(count1 -> {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target.: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
}
});
});
});
System.out.println();
});
...但是,正如其他人指出的那样,您的限制使某些数量的解决方案变得不可能。
这是该主题的变体,为所有 N 提供解决方案...
IntStream.rangeClosed(1,100).forEach(target -> {
final int max1 = (target + 1 - 1) / 1;
final int max2 = (target + 2 - 1) / 2;
final int max5 = (target + 5 - 1) / 5;
for (int count1= max1; count1 > 0; count1--) {
for (int count2=Math.min(max2,count1 - 1); count2 > 0; count2--) {
for (int count5=Math.min(max5,count2 - 1); count5 > 0; count5--) {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target..............: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
return; // Note.: solution found,exit from Consumer,NOT Method!
}
}
}
}
/*
* Fallback 1: at least one of each denomination...
*/
for (int count1=max1; count1 > 0; count1--) {
for (int count2=max2; count2 > 0; count2--) {
for (int count5=max5; count5 > 0; count5--) {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target (fallback 1).: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
return; // Note.: solution found,NOT Method!
}
}
}
}
/*
* Fallback 2: "anything goes"...
*/
for (int count1=max1; count1 >= 0; count1--) {
for (int count2=max2; count2 >= 0; count2--) {
for (int count5=max5; count5 >= 0; count5--) {
final int sum = count5*5 + count2*2 + count1*1;
if (sum == target) {
System.out.println("Target (fallback 2).: " + target + " -> " + count5 + "*5$ " + count2 + "*2$ " + count1 + "*1$");
return; // Note.: solution found,NOT Method!
}
}
}
}
System.out.println("Target..............: " + target + " NO Solution possible!");
});