简体   繁体   English

递归打印出等于给定总和的数组中的所有子集不会跳转到下一个迭代

[英]Print out all subsets in an array that equal an given sum recursively does not jump to the next iteration

I'm trying to print out all the subsets that is equal to a given sum using recursion. 我正在尝试使用递归打印出所有等于给定总和的子集。 However, my code doesn't jump to the next iteration after the first one is finished: 但是,我的代码在第一个迭代完成后不会跳转到下一个迭代:

import java.util.*;
public class Combinations {
public static int currentSum = 0;
public static ArrayList<Integer> usedItems = new ArrayList<>();
public static void main( String[] args ) throws Exception {

    int arr[] = {1, 2, 3, 4, 2};
    int sum = 6;
    printCombinations(arr, sum);
}

public static void printCombinations(int[] availableItems, int goal){

    for (int i = 0; i < availableItems.length; i++){
        if (currentSum + availableItems[i] == goal){
            System.out.println(Arrays.toString(usedItems.toArray()) + availableItems[i]);
            currentSum = 0;
            usedItems.clear();
        }
        if(currentSum + availableItems[i] > goal){
            continue;
        }
        if(currentSum + availableItems[i] < goal){
            currentSum += availableItems[i];
            usedItems.add(availableItems[i]);
            int[] newAvailableItems = Arrays.copyOfRange(availableItems, 1, availableItems.length);
            printCombinations(newAvailableItems, goal);
        }
    }
}

For example if the sum equals 6 the program only prints out 1, 2, 3 but not jump to the next number and check from there. 例如,如果总和等于6,则程序仅打印出1、2、3,而不会跳到下一个数字并从那里检查。

I think the problem is here: 我认为问题出在这里:

 if (currentSum + availableItems[i] == goal)

For the given array int arr[] = {1, 2, 3, 4, 2}; 对于给定的数组, int arr[] = {1, 2, 3, 4, 2}; and goal value int sum = 6; 目标值int sum = 6;

Iterations: 迭代次数:

  1. currentSum = 0, availableItems[i] = 1 (0 + 1 = 1) fits -> if(currentSum + availableItems[i] < goal) currentSum = 0, availableItems[i] = 1 (0 +1 = 1)适合-> if(currentSum + availableItems[i] < goal)
  2. currentSum = 1, availableItems[i] = 2 (1 + 2 = 3) fits -> if(currentSum + availableItems[i] < goal) currentSum = 1, availableItems[i] = 2 (1 + 2 = 3)适合-> if(currentSum + availableItems[i] < goal)
  3. currentSum = 3, availableItems[i] = 3 (3 + 3 = 6) fits -> if (currentSum + availableItems[i] == goal) and in the same iteration: currentSum = 0, availableItems[i] = 3 (0 + 3 = 3) fits -> if (currentSum + availableItems[i] < goal) currentSum = 3, availableItems[i] = 3 (3 + 3 = 6)适合-> if (currentSum + availableItems[i] == goal)并且在同一迭代中: currentSum = 0, availableItems[i] = 3 (0 + 3 = 3)适合-> if (currentSum + availableItems[i] < goal)
  4. currentSum = 3, availableItems[i] = 4 (3 + 4 = 7) fits -> if(currentSum + availableItems[i] > goal) and so on... currentSum = 3, availableItems[i] = 4 (3 + 4 = 7)适合-> if(currentSum + availableItems[i] > goal) ,依此类推...

For the given array and sum value is not going to fit the first if never. 对于给定的数组,求和值将永远不会适合第一个(如果永不)。

You need increase the iterator: 您需要增加迭代器:

if (currentSum + availableItems[i] == goal){
    System.out.println(Arrays.toString(usedItems.toArray()) + availableItems[i]);
    currentSum = 0;
    usedItems.clear();
    i++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM