A set of Java, an array of any number of digits equal to the sum of a specified number

A set of digital numbers together and any number equal to a specified number. Such as a digital set 1,2,3, 4,5,6, 6 list all possibilities of digital sum is equal, then the results are: 1 + 2 + 3, 2 + 4, where the results of the main array is not scrambled addition, but rather, any number of addition and subtraction in accordance with the order of the array, all we see the results of only 1 + 2 + 3 + 3 + 1 without the 3 + 2 or 2 + 1

 

step1, entity classes:

static class TestDTO  {
     String id; //id
    Integer num;//数字
     public String getId() {
        return id;
     }
    public void setId(String id) {
        this.id = id;
    }
    public Integer getner () {
         return num;
    }
 
    public  void setne (String num) {
         this .num = num;
    }

}

 

step2, implementation method:

public  static List<List<TestDTO>>  Test(List<TestDTO> dtoParam,Integer samplesNumber) {
    List<List<TestDTO>>  reust = new ArrayList<List<TestDTO>>();
    int a=1;
    int c=1;
    List<TestDTO> d=null;
    for (int i = 0; i < dtoParam.size(); i++) {
        int s =  dtoParam.get(i).getNum();
       STR the StringBuffer = new new the StringBuffer (dtoParam.get (I) .getNum () + "+"); // for printing console display, regardless of the logic and 
        Boolean BB = to true ;
         the while (BB) {
             IF (= BB to false ) {
                 BREAK ;
            }
            if(dtoParam.size()==a){
                bb=false;
                break;
            }
            boolean b=true;
            while(b){
                if(dtoParam.size()==c){
                    a++;
                    b=false;
                    c=a;
                    break;
                }
                d = new ArrayList<TestDTO>();
                d.add(dtoParam.get(i));
                for(int j=c;j<dtoParam.size();j++){
                    s = s + dtoParam.get(j).getNum();
                    d.add(dtoParam.get(j));
                     str.append (dtoParam.get (J) .getNum () + "+"); // for printing console display, and irrespective of the logic 
                    System.out.println (str.substring (0, str.length ( ) - 1)); // for printing console display, and irrespective of the logic 
                    IF (S == samplesNumber) {
                        reust.add(d);
                        break;
                    }
                    if(dtoParam.size()-j==1){
                        s =  dtoParam.get(i).getNum();
                        STR = new new the StringBuffer (dtoParam.get (I) .getNum () + "+"); // for printing console display, regardless of the logic and 
                        C ++ ;
                         BREAK ;
                    }
                }
            }
        }
    }
    return  reust;
}

 

step3, test methods:

 

 public  static  void main (String [] args) {
  // simulate a set of digital
    // analog digital set a 
        List <TestDTO> L = new new the ArrayList <TestDTO> ();
         for ( int I =. 1; I <10; I ++ ) {
            TestDTO d=new TestDTO();
            d.setId(i+"");
            d.setNum (i);
            l.add(d);
        }
        List<List<TestDTO>>  list=   Test(l,6);
        for (int i=0;i<list.size();i++){
            String str="";
            for(int j=0;j<list.get(i).size();j++){
                str=str+list.get(i).get(j).getNum()+"+";
            }
            System.out.println ( "first" + i + "Result:" + str.substring (0, str.length () -. 1 ));
        }

}

 

step4, output:

All console output a digital added
 
1+2
1+2+3
1+2+3+2
1+2+3+2+3
1+2+3+2+3+4
1+2+3+2+3+4+5
1+2+3+2+3+4+5+6
1+2+3+2+3+4+5+6+7
1+2+3+2+3+4+5+6+7+8
1+2+3+2+3+4+5+6+7+8+9
1+3
1+3+4
1+3+4+5
1+3+4+5+6
1+3+4+5+6+7
1+3+4+5+6+7+8
1+3+4+5+6+7+8+9
1+4
1+4+5
1+4+5+6
1+4+5+6+7
1+4+5+6+7+8
1+4+5+6+7+8+9
1+5
1+5+5
1+5+5+6
1+5+5+6+7
1+5+5+6+7+8
1+5+5+6+7+8+9
1+6
1+6+7
1+6+7+8
1+6+7+8+9
1+7
1+7+8
1+7+8+9
1+8
1+8+9
1+9
2+3
2+3+4
2+3+4+5
2+3+4+5+6
2+3+4+5+6+7
2+3+4+5+6+7+8
2+3+4+5+6+7+8+9
2+4
2+4+4
2+4+4+5
2+4+4+5+6
2+4+4+5+6+7
2+4+4+5+6+7+8
2+4+4+5+6+7+8+9
2+5
2+5+6
2+5+6+7
2+5+6+7+8
2+5+6+7+8+9
2+6
2+6+7
2+6+7+8
2+6+7+8+9
2+7
2+7+8
2+7+8+9
2+8
2+8+9
2+9
3+4
3+4+5
3+4+5+6
3+4+5+6+7
3+4+5+6+7+8
3+4+5+6+7+8+9
3+5
3+5+6
3+5+6+7
3+5+6+7+8
3+5+6+7+8+9
3+6
3+6+7
3+6+7+8
3+6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