【问题标题】:Determine the combinations of making change for a given amount确定对给定金额进行更改的组合
【发布时间】:2012-03-20 01:38:59
【问题描述】:

我的任务是编写一个算法,使用蛮力来确定不同方式的数量,以及给定数量的相关变化组合。零钱将使用以下硬币产生:便士(1 美分)、镍(5 美分)、一角硬币(10 美分)和 25 美分(25 美分)。

例如

输入:16(表示变化 16 美分)

输出:可以通过 6 种不同的方式产生,它们是:

  1. 16 便士。
  2. 11 便士,1 镍
  3. 6 便士,1 毛钱
  4. 6便士,2镍
  5. 1便士,3镍
  6. 1 便士、1 镍、1 毛钱

我的算法必须为指定的变化量产生所有可能的变化组合。


我完全不知道如何开始这样的算法。任何能让我前进的意见或见解都会很棒。

【问题讨论】:

标签: algorithm


【解决方案1】:
public class PrintAllCoinCombinations {


    static int findChange(int arr[], int index , int value, String str){

        if(value == 0){
            System.out.println(str);
            return 1;
        }
        if(index<0){
            return 0;
        }
        if(value<0){
            return 0;
        }

        int excl  = findChange(arr,index-1,value,str);

        str += " "+ arr[index];

        int incl = findChange(arr,index,value-arr[index],str);

        return incl + excl;

    }

    public static void main(String [] arg){
        int arr[] = {1,5,10,25};
        String s = "";
        int result = findChange(arr,3,16,s);
        System.out.println(result);
    }
}

【讨论】:

    【解决方案2】:

    好吧,如果你想要蛮力解决方案,你可以从一个非常幼稚的 recursive approach 开始。但为了提高效率,您需要dynamic programming approach

    对于递归方法:

    1. find out the number of ways you can make using penny only.
    2. do the same using penny and nickel only. (this includes step 1 also)
    3. the same using penny, nickel and dime only (including step 2).
    4. using all the coins (with all previous steps).
    

    第 1 步很简单,只有一种方法可以做到。

    对于第 2 步,递归应该是这样的:

    number of ways to make n cent using penny and nickel = 
        number of ways to make (n - [1 nickel]) using penny and nickel
        + number of ways to make n cent using penny only
    

    第 3 步:

    number of ways to make n cent using penny, nickel and dime = 
        number of ways to make (n - [1 dime]) using penny, nickel and dime
        + number of ways to make n cent using penny and nickel only
    

    第 4 步类似。

    还有一件事要记住:您可以通过一种方式(即使用零硬币)赚取 0 美分,这是基本情况。

    【讨论】:

      【解决方案3】:

      好的。让我解释一下蛮力算法的一个想法。我将在这里使用递归。

      让您需要更改c 美分。然后将c视为

      c = p * PENNY + n * NICKEL + d * DIME + q * QUARTER
      

      或者简单地说,

      c = ( p * 1 ) + ( n * 5 ) + ( d * 10 ) + ( q * 25 )
      

      现在您需要检查pndq 的所有可能值,它们等于c 的值。使用递归,对于每个p in [0, maximumPennies],遍历每个n in [0, maximumNickels]。对于每个n,遍历每个d in [0, maximumDimes]。对于每个d,遍历每个q in [0, maximumQuarters]

      p in [0, maximumPennies] AND c >= p
        |
        +- n in [0, maximumNickels] AND c >= p + 5n
             |
             +- d in [0, maximumDimes] AND c >= p + 5n + 10d
                  |
                  +- q in [0, maximumQuarters] AND c >= p + 5n + 10d + 25q
      

      对于这些步骤中的任何相等,您都有一个解决方案。

      【讨论】:

        【解决方案4】:

        你可以开始思考这个问题,把它分成子问题解决这些问题,然后改变问题并调整你的解决方案。

        在您的情况下,您可以首先尝试仅使用便士解决问题(当然只有一个明显的解决方案),然后查看镍币和便士并查看那里的所有组合等等。为了改进这一点,您可以重用算法早期阶段的解决方案。

        【讨论】:

          【解决方案5】:

          尝试在这个上使用递归。 你的函数应该有两个参数——你被允许使用的最大值和剩余的支付金额(你需要第一个以避免重复)。 以这样的方式制作函数:如果它是在平凡的情况下(例如 1、5、10 并且您可以分别拿一分钱、镍、一角硬币),则打印平凡的解决方案。此外,对于每种情况,请尝试从所有允许的类型中取出一枚硬币(例如,不大于允许的最大值)并递归继续。

          希望这会有所帮助。

          【讨论】:

            猜你喜欢
            • 2017-07-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-08-20
            • 2013-01-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多