如何在一个数组中求出任意几个数的和等于给定数


代码:

import java.util.*;
 
public class Organize1 {
    static int[] iArray = {0, 2, 3, 1, 4, 10, 23, 7, 8, 9, 6, 3};
    static ArrayList<String> list = new ArrayList<String>();
    static Set<String> index = new TreeSet<String>();
    static StringBuilder str;
    static StringBuilder indexStr;
    static int sum;
     
    public static void org(int[] iArray, int start){
        for(int i=0; i<iArray.length; i++){
            sum = iArray[i];
            str = new StringBuilder();
            str.append(iArray[i]);
            indexStr = new StringBuilder();
            indexStr.append(i);
 
            for(int j=start; j<iArray.length; j++){                                   
                if(i != j){
                    sum += iArray[j];
                     
                    if(sum == 10){
                        str.append("+"+ iArray[j]);
                        indexStr.append("+"+ j);
                        int size = index.size();
                        index.add(indexStr.toString());
                         
                        if(index.size() > size)
                            list.add(str.toString());                           
                    }
                    if(sum < 10){
                        str.append("+"+ iArray[j]);
                        indexStr.append("+"+ j);
                    }
                    if(sum > 10){                       
                        sum -= iArray[j];                           
                    }
                }
            }
        }
    }
     
    public static void main(String args[]){   
        for (int i=0; i<iArray.length; i++)
            org(iArray, i);
        System.out.println(list.toString());
    }
}

结果

[0+2+3+1+4, 2+0+3+1+4, 3+0+2+1+4, 1+0+2+3+4, 4+0+2+3+1, 10+0, 7+0+2+1, 8+0+2, 9+0+1, 2+3+1+4, 3+2+1+4, 1+2+3+4, 4+2+3+1, 7+2+1, 8+2, 9+1, 7+3, 6+3+1, 2+1+4+3, 6+1+3, 0+4+6, 3+4+3, 4+6, 7+3, 6+4, 0+10, 3+7, 3+7, 0+7+3, 2+8, 1+9, 1+6+3]



(1)