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

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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能