【问题标题】:Why does my recursive solution print duplicates?为什么我的递归解决方案会打印重复项?
【发布时间】:2011-09-08 21:01:53
【问题描述】:

问题是:

编写一个静态方法 makeChange,该方法使用递归回溯找到所有方法,使用便士(1 美分)、镍(5 美分)、角钱(10 美分)和 25 美分(25 美分)为给定金额找零.例如,当找 37 美分时,您可以使用:1 夸特、1 角钱和 2 便士; 3角钱和7便士;或其他组合。

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

System.out.println("P N D Q");
System.out.println("------------");
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]

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

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) 产生以下输出:

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

附:这不是布置的作业,这是自愿练习

【问题讨论】:

  • 如果你也显示你的输出会有帮助。
  • 您在代码中的哪个位置检查该组合是否已被记录?我没看到,但可能只是我的眼睛。
  • 从这段代码中得到重复的输出是有道理的。我不确定你能避免它。您可以跟踪以前的解决方案而不将它们打印出来,但这在此递归代码中会非常混乱。
  • 没有 1 票,没有接受,没有 1 个答案?
  • 然而,可能有一种完全不同的方式来使用不会产生重复的递归。

标签: java recursion


【解决方案1】:

每次调用递归函数时,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]

【讨论】:

  • 谢谢。但是如何颠倒打印顺序以使其与预期输出相匹配?如果你只是颠倒遍历的顺序而使用相同的过程,它是行不通的。
  • 更新了反向输出的功能
猜你喜欢
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多