java - 打印您可以按照所述支付金额的方式数量
问题描述
有多少种方式,你可以用 1 美元、2 美元和 5 美元面额支付 N 美元,以这样的方式
- 1美元硬币的数量总是大于2美元硬币的数量,2美元硬币的数量总是大于5美元硬币的数量。
- 注意:每个面额至少应提供一枚硬币。
- 约束 1 <= N <= 100
我尝试过这样的事情,但无法获得如何检查约束
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);}
解决方案
你可以尝试这样的事情:
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, exit from Consumer, 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, exit from Consumer, NOT Method!
}
}
}
}
System.out.println("Target..............: " + target + " NO Solution possible!");
});
推荐阅读
- sql - SQL:多列的直方图
- java - 如何按对象内部的对象字段排序?
- c# - 客户端在连接到 Azure SQL 数据库时遇到问题
- javascript - req.body 抛出一个空的 {}
- r - 您如何检查 R Shiny 中的所有后代是否都是干净的(未失效)?
- c++ - 如何乘以 std::vector
通过 int 向量的每个元素应该是一位数? - python - 如何编写一个函数,它接受一个文件名并返回一个字符串,其中所有 \n 字符都替换为“_”
- apache-kafka - 使用 Avro 从 Logastash 发送到 Kafka
- java - JPA - 休眠 - '字段列表'中的未知列'x_xxx_xxxxx'
- linux - 如何安装ubuntu统一