打印您可以按照描述支付金额的方式数量

如何解决打印您可以按照描述支付金额的方式数量

有多少种方式,你可以用 1 美元、2 美元和 5 美元的面额支付 N 美元,这样

  • 1 美元硬币的数量总是大于 2 美元硬币的数量,而 2 美元硬币的数量总是大于 5 美元硬币的数量
  • 注意:每个面额至少应有一枚硬币。
  • 约束条件 1

我尝试过类似的方法,但无法了解如何检查约束

    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 = 1numTwos = 2numOnes = 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!");
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?