Java 集合、數組 任意個數數字相加等於一個指定的數


一組數字 任意個數數字相加的和等於指定的數字。  比如數字集合 1,2,3, 4,5,6  ,列出所有數字相加等於6的可能性,那么結果有:1+2+3,2+4, 主要這里的結果不是數組打亂順序相加,而是按照數組順序取任意個數相加減,所有大家看到結果只有1+2+3而沒有1+3+2或則3+2+1

 

step1、實體類:

static class TestDTO  {
     String id; //id
    Integer num;//數字
     public String getId() {
        return id;
     }
    public void setId(String id) {
        this.id = id;
    }
    public Integer getNum() {
        return num;
    }
 
    public void setNum(Integer num) {
        this.num = num;
    }

}

 

step2、實現方法:

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();
       StringBuffer   str=new StringBuffer(dtoParam.get(i).getNum()+"+");//用於控制台打印顯示,和邏輯無關
        boolean bb=true;
        while(bb){
            if(bb=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()+"+");//用於控制台打印顯示,和邏輯無關
                    System.out.println(str.substring(0,str.length()-1));//用於控制台打印顯示,和邏輯無關
                    if (s == samplesNumber) {
                        reust.add(d);
                        break;
                    }
                    if(dtoParam.size()-j==1){
                        s =  dtoParam.get(i).getNum();
                        str=new StringBuffer(dtoParam.get(i).getNum()+"+");//用於控制台打印顯示,和邏輯無關
                        c++;
                        break;
                    }
                }
            }
        }
    }
    return  reust;
}

 

step3、測試方法:

 

 public static void main(String[] args) {
 //模擬一個數字集合
   //模擬一個數字集合
        List<TestDTO> l=new 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("第"+i+"個結果:"+str.substring(0,str.length()-1));
        }

}

 

step4、輸出結果:

控制台輸出所有數字相加的情況
 
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+7+8+9
3+7
3+7+8
3+7+8+9
3+8
3+8+9
3+9
4+5
4+5+6
4+5+6+7
4+5+6+7+8
4+5+6+7+8+9
4+6
4+6+7
4+6+7+8
4+6+7+8+9
4+7
4+7+8
4+7+8+9
4+8
4+8+9
4+9
5+6
5+6+7
5+6+7+8
5+6+7+8+9
5+7
5+7+8
5+7+8+9
5+8
5+8+9
5+9
6+7
6+7+8
6+7+8+9
6+8
6+8+9
6+9
7+8
7+8+9
7+9
8+9
 
 
最后篩選結果:
第0個結果:1+2+3=6
第1個結果:1+5=6
第2個結果:2+4=6

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM