首页 > 解决方案 > 打印您可以按照所述支付金额的方式数量

问题描述

有多少种方式,你可以用 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);}

标签: javamathlogicreasoning

解决方案


你可以尝试这样的事情:

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!");
});

推荐阅读