| 我想找到所有通过回溯求和n的整数子集 例如,整数:
1 2 3 4 5 6 7
并且n = 7 我想输出
1 2 4
1 6
2 5
3 4
7 
我认为我应该将我正在评估的整数数组中的位置作为参数传递,但是我仍然在写其余的逻辑。 到目前为止,我的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author talleres
 */
public class Main {




int sum (TreeSetts, int temp) {

    int sum=0;

    for (Integer i: ts){

        sum +=i;

    }

    return sum+temp;
}



static HashSet> alternatives = new HashSet >();
static ArrayList> subsets = new ArrayList >();

static TreeSet getNextSubset (){

    TreeSet alternative = new TreeSet();

    if (!alternatives.contains(alternative)){   
        return alternative;   
    }
    else return null; // BEWARE!! 
} 

static void findSubsets (ArrayList numbers, int amount, int index){


    TreeSet  subset = new TreeSet();

    int temp = numbers.get(index); //initialize alternative

    if (temp<=amount)
        subset.add(temp);

    if (temp==amount)
        subsets.add(subset);




}


    public static void main(String[] args) throws IOException {
        // TODO code application logic here

        BufferedReader br = new BufferedReader(new InputStreamReader(www.sxzhongrui.com));
        System.out.println(\"inset integers\");

        ArrayList numeros = new ArrayList();
        String line=br.readLine();

        while (!line.equals(\"\")){
            numeros.add (Integer.parseInt(line));
            line = br.readLine();
        }

        Collections.sort(numeros);

        System.out.println(\"insert the amount the subsets should sum\");
           line = br.readLine();

        int amount = Integer.parseInt(line);

        ArrayList accum = new ArrayList();

        findSubsets(numeros, amount, 0);


    }

}