MY_MAIN 2019-01-29 10:59 采纳率: 66.7%
浏览 2434
已采纳

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]

  • 写回答

3条回答 默认 最新

  • echo_wjcwjc 2019-01-29 12:39
    关注

    有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;
        }
    
    }
    
    
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?