java 从数组取出指定数量的值,相加大于等于或小于等于指定的值,取出对应的组合下标,下标不能重复

Integer[] datas = new Integer[]{1,2,3,4,5,6,7,8,9,10,11};//目标数组
Integer min = 6;//大于等于的值
Integer max = 12;//小于等于的值
Integer count = 3;//指定数量

根据count如:3,3个数相加大于等于min小于等于max,2个数相加大于等于min小于等于max,1个数相加大于等于min小于等于max。
如果count=2,2个........,1ge.........。
返回List。

下面代码是我写死,我想知道怎样写活【count】

public static void main(String[] args) {
        Integer[] datas = new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
        Integer min = 6;
        Integer max = 12;
        Integer count = 3;
        List<Integer[]> test = test(datas, min, max, count);
        for(Integer[] integers : test){
            for(Integer t : integers){
                System.out.print("["+t+"]");
            }
            System.out.println("");
        }

    }
    public static List<Integer[]> test(Integer[] datas, Integer min, Integer max, Integer count){
        List<Integer[]> result = new ArrayList<>();
        switch (count){
            case 1:
                result.addAll(one(datas, min, max));
                break;
            case 2:
                result.addAll(one(datas, min, max));
                result.addAll(two(datas, min, max));
                break;
            case 3:
                result.addAll(one(datas, min, max));
                result.addAll(two(datas, min, max));
                result.addAll(three(datas, min, max));
                break;
        }
        return result;
    }

    public static List<Integer[]> one(Integer[] datas, Integer min, Integer max){
        List<Integer[]> result = new ArrayList<>();
        for(int i = 0,len = datas.length; i < len; i++){
            if(datas[i] >= min && datas[i] <= max){
                result.add(new Integer[]{i});
            }
        }
        return result;
    }

    public static List<Integer[]> two(Integer[] datas, Integer min, Integer max){
        List<Integer[]> result = new ArrayList<>();
        for(int i = 0,len = datas.length; i < len; i++){
            for(int j = 1 + i; j < len; j++){
                Integer num = datas[i] + datas[j];
                if(num >= min && num <= max){
                    result.add(new Integer[]{i,j});
                }
            }
        }
        return result;
    }

    public static List<Integer[]> three(Integer datas[], Integer min, Integer max){
        List<Integer[]> result = new ArrayList<>();
        for(int i = 0,len = datas.length; i < len; i++){
            for(int j = 1 + i; j < len; j++){
                for(int k = 1 + j; k < len; k++){
                    Integer num = datas[i] + datas[j] + datas[k];
                    if(num >= min && num <= max){
                        result.add(new Integer[]{i,j,k});
                    }
                }
            }
        }
        return result;
    }

输出的【下标】组合

[5]
[6]
[7]
[8]
[9]
[10]
[11]
[0][4]
[0][5]
[0][6]
[0][7]
[0][8]
[0][9]
[0][10]
[1][3]
[1][4]
[1][5]
[1][6]
[1][7]
[1][8]
[1][9]
[2][3]
[2][4]
[2][5]
[2][6]
[2][7]
[2][8]
[3][4]
[3][5]
[3][6]
[3][7]
[4][5]
[4][6]
[0][1][2]
[0][1][3]
[0][1][4]
[0][1][5]
[0][1][6]
[0][1][7]
[0][1][8]
[0][2][3]
[0][2][4]
[0][2][5]
[0][2][6]
[0][2][7]
[0][3][4]
[0][3][5]
[0][3][6]
[0][4][5]
[1][2][3]
[1][2][4]
[1][2][5]
[1][2][6]
[1][3][4]
[1][3][5]
[2][3][4]

有2种方法第一种就是再开辟一个与datas相同长度的数组(datax)。这个数组里面都是二进制。假设datas的长度,datax长度为3
将datax从 000 001 010 011 100 101 110 111 只要末尾不断加1再不超过2
然后将datas的相应位置与datax的相应位置的数据相乘
若datas数据为 1 2 3 min -- 2 max --4 count -- 2
datax 000 001 010 011 100 101 110 111
sum 0 1*3 1*2 1*2+1*3 1*1 1*1+1*3 1*1+1*2 1*1+1*2+1*3
只要sum>=min&&sum<=max至于count只要在符合sum的前提下datax的1的个数<=count就行

另外一种是回溯法原理和上面类似





import java.util.ArrayList;
import java.util.List;

public class A {
    public static Integer[] datas = new Integer[]{1,2,3,4,5,6,7,8,9,10,11};
    public static Integer[] datasx = new Integer[datas.length];
    public static Integer y = 100;
    public static void main(String[] args) {

        Integer min = 6;
        Integer max = 12;
        Integer count = 3;
        List<List<Integer>> test = three(datas, min, max, count);


        for(List<Integer> list : test){
            for(Integer t:list){
                System.out.print("["+t+"]");
            }
            System.out.println();
        }


       // dox(32,min,max,3);

    }



    public static List<List<Integer>> three(Integer datas[], Integer min, Integer max,Integer count){
        List<Integer> result = new ArrayList<Integer>();
        List<List<Integer>> list  = new ArrayList<List<Integer>>();
        double b = (double)datas.length;
        Integer u = (int)Math.pow(2.0, b)-1;
        System.out.println("wjc"+u);
        int y = 0;
        while(y < u){
            y+=1;
            result = dox(y,min,max,count);
            if(result != null){
                list.add(result);
            }

        }
        return list;
    }
    private static List<Integer> dox(int parseInt,Integer min, Integer max,Integer count) {
        // TODO Auto-generated method stub

         List<Integer> result1 = new ArrayList<>();
         int sum = 0;
         int j = 0;
         int y = 0;
         int p;
         for(int i = 0;i<datasx.length;i++){
             datasx[i] = 0;
         }

         if(y>count){
             return null;
         }

         while(parseInt>0){
             p = parseInt%2;
             parseInt = parseInt/2;
             if(p == 1){
                 y++;
                 result1.add(datas.length-1-j);
             }
             datasx[j++] = p; 
         } 




         for(int i = datasx.length-1;i>=0;i--){
        //  System.out.print(datasx[i]);
             sum += datas[datas.length-1-i]*datasx[i];
         }
        //System.out.println();
        //System.out.println(sum);
         if(sum >= min && sum <=max){
             return result1;
         }
         return null;
    }

}


上面的代码是我说的第一种方法

这是我的答案:你看看满足你的要求不:

Integer[] datas = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};// 目标数组
        Integer min = 6;// 大于等于的值
        Integer max = 12;// 小于等于的值
        Integer count = 3;// 指定数量
        // 遍历cont为1
        for (int i = 0; i < datas.length; i++) {
            if (min<=datas[i]&&datas[i]<=max) {
                System.out.println(i);
            }
        }
        // count 为2 时候
        for (int i = 0; i < datas.length; i++) {
            for (int j = i+1; j < datas.length; j++) {
                int sum2=datas[i]+datas[j];
                if (min<=sum2&&sum2<=max) {
                    System.out.println("第一个下标为:"+i+"第二个下标为:"+j);
                }

            }

        }
    // count 为3的时候
        for (int i = 0; i < datas.length; i++) {
            for (int j = i+1; j < datas.length; j++) {
                for (int k = j+1; k < datas.length; k++) {

                    int sum3=datas[i]+datas[j]+datas[k];
                    if (min<=sum3&&sum3<=max) {
                        System.out.println("第一个下标为:"+i+"第二个下标为:"+j+"第三个下标是:"+k);
                    }
                }

            }

        }

我明白你的想法,但是这样的算法是不行的,试想一下如果count是10,那你不是要写10个for循环来算10个数的和,并没有“写活”count让他自动就有10次for的方法(至少我没想到)。这道题应该是用搜索来做,个人感觉用深度搜素应该可以满足要求。