问题是:

编写一个静态方法makeChange,该方法使用递归回溯找到所有方法,以美分(1美分),镍(5美分),角钱(10美分)和四分之一(25美分)进行给定金额的更改。例如,以37美分进行找零时,您可以使用:1个季度,1个角钱和2个便士; 3角钱和7便士;或其他组合。

您的方法应接受一个参数:进行更改的美分量。方法的输出应该是每种硬币的所有组合的序列,这些组合加起来等于该数量,每行一个。例如,如果客户端代码包含以下调用:

System.out.println(“ P N D Q”);
System.out.println(“ ------------”);
makeChange(28);

生成的总输出应为以下内容:

压力
------------
[3,0,0,1]
[3,1,2,0]
[3,3,1,0]
[3,5,0,0]
[8,0,2,0]
[8,2,1,0]
[8,4,0,0]
[13,1,1,0]
[13,3,0,0]
[18,0,1,0]
[18,2,0,0]
[23,1,0,0]
[28,0,0,0]

我的解决方案是保留一个与四种面额相对应的整数列表。一种单独的方法处理列表中“硬币”的总价值。递归方法将每种面额的“硬币”数相加,直到总和等于给定值为止,然后打印出来。不幸的是,我的解决方案对每种可能的硬币组合都打印了许多副本。为什么?

public static void makeChange(int n) {
   ArrayList<Integer> combos = new ArrayList<Integer>();
   combos.add(0);
   combos.add(0);
   combos.add(0);
   combos.add(0);
   System.out.println(" P  N  D  Q");
   System.out.println("------------");
   makeChange(n, combos);
}

public static void makeChange(int n, ArrayList<Integer> combos) {
   int sum = getSum(combos);
   if (sum == n) {
      System.out.println(combos.toString());
   } else {
      for (int i = 0; i < combos.size(); i++) {
         combos.set(i, combos.get(i) + 1);
         if (getSum(combos) <= n) {
            makeChange(n, combos);
         }
         combos.set(i, combos.get(i) - 1);
      }
   }
}

public static int getSum(ArrayList<Integer> combos) {
   int sum = combos.get(0);
   sum += 5 * combos.get(1);
   sum += 10 * combos.get(2);
   sum += 25 * combos.get(3);
   return sum;
}


输出示例:

调用makeChange(6)产生以下输出:

压力
------------
[6,0,0,0]
[1、1、0、0]
[1、1、0、0]
附言这不是家庭作业,是自愿练习

最佳答案

每次调用递归函数时,for循环会将i变量重置为零,这将导致重复输出。

您需要将此变量作为输入参数传递:

public static void makeChange(int n) {
   List<Integer> combos = new ArrayList<Integer>();  // you should declare as List, not ArrayList
   combos.add(0);
   combos.add(0);
   combos.add(0);
   combos.add(0);
   System.out.println(" P  N  D  Q");
   System.out.println("------------");
   makeChange(n, 0, combos);
}

public static void makeChange(int n, int i, List<Integer> combos) {
   int sum = getSum(combos);
   if (sum == n) {
      System.out.println(combos.toString());
   } else {
       while (i < combos.size()) {
         combos.set(i, combos.get(i) + 1);
         if (getSum(combos) <= n) {
            makeChange(n, i, combos);
         }
         combos.set(i, combos.get(i) - 1);
         ++i;
      }
   }
}


对于makeChange(28),将输出:

 P  N  D  Q
------------
[28, 0, 0, 0]
[23, 1, 0, 0]
[18, 2, 0, 0]
[18, 0, 1, 0]
[13, 3, 0, 0]
[13, 1, 1, 0]
[8, 4, 0, 0]
[8, 2, 1, 0]
[8, 0, 2, 0]
[3, 5, 0, 0]
[3, 3, 1, 0]
[3, 1, 2, 0]
[3, 0, 0, 1]


对于makeChange(6),它输出:

 P  N  D  Q
------------
[6, 0, 0, 0]
[1, 1, 0, 0]


请记住,P
编辑
要反转输出,您可以先将输出写入列表,然后在完成该过程后输出列表:

public static void makeChange(int n) {
   List<Integer> combos = new ArrayList<Integer>();
   List<String> output = new ArrayList<String>();
   combos.add(0);
   combos.add(0);
   combos.add(0);
   combos.add(0);
   System.out.println(" P  N  D  Q");
   System.out.println("------------");
   makeChange(n, 0, combos, output);
   for(String s: output) {
       System.out.println(s);
   }
}

public static void makeChange(int n, int i, List<Integer> combos, List<String> output) {
   int sum = getSum(combos);
   if (sum == n) {
       output.add(0, combos.toString());
   } else {
       while (i < combos.size()) {
         combos.set(i, combos.get(i) + 1);
         if (getSum(combos) <= n) {
            makeChange(n, i, combos, output);
         }
         combos.set(i, combos.get(i) - 1);
         ++i;
      }
   }
}


现在,对于makeChange(28),输出为:

 P  N  D  Q
------------
[3, 0, 0, 1]
[3, 1, 2, 0]
[3, 3, 1, 0]
[3, 5, 0, 0]
[8, 0, 2, 0]
[8, 2, 1, 0]
[8, 4, 0, 0]
[13, 1, 1, 0]
[13, 3, 0, 0]
[18, 0, 1, 0]
[18, 2, 0, 0]
[23, 1, 0, 0]
[28, 0, 0, 0]

10-06 08:54