Java递归实现多个二维数组的组合

Java递归实现多个二维数组的组合

题目要求
根据下图数据,实现随机组合,平均到期年限小于等于10,组合里边的序号必须是各个领域都有,所以必须是五个及以上的组合
在这里插入图片描述
以下是用Java实现题目要求

import java.util.*;

public class Demo {
    public static void main(String[] args) {
        //定义五个二维数组代表债券领域
        //定义医药健康数据二维数组
        int[][] array1 = {{2,1},{9,2},{20,3}};
        //定义交通运输数据二维数组
        int[][] array2 = {{12,5},{25,6}};
        //定义科技研发数据二维数组
        int[][] array3 = {{4,7},{15,8},{20,9}};
        //定义装备制造数据二维数组
        int[][] array4 = {{4,10},{9,11},{18,12}};
        //定义国民福利数据二维数组
        int[][] array5 = {{18,15}};

        //存储结果集合
        Map<String, Object> resultMap = new LinkedHashMap();
        //先确定五个类别都只取一个的情况
        for(int i1 = 0 ; i1<array1.length ; i1++){
            for(int i2 = 0 ; i2<array2.length ; i2++){
                for(int i3 = 0 ; i3<array3.length ; i3++){
                    for(int i4 = 0 ; i4<array4.length ; i4++){
                        for(int i5 = 0 ; i5<array5.length ; i5++){
                            if(array1[i1][0]+array2[i2][0]+array3[i3][0]+array4[i4][0]+array5[i5][0]<=50){
                                Integer num = array1[i1][0]+array2[i2][0]+array3[i3][0]+array4[i4][0]+array5[i5][0];
                                //定义一个列表剔除五个值
                                List<Integer> list = new ArrayList<>();
                                list.add(array1[i1][1]);
                                list.add(array2[i2][1]);
                                list.add(array3[i3][1]);
                                list.add(array4[i4][1]);
                                list.add(array5[i5][1]);
                                Collections.sort(list);
                                //调用递归方法
                                resultMap.put(list.toString(),num);
                                removePart(list,num,resultMap);
                            }
                        }
                    }
                }
            }
        }
        //输出结果
        if(resultMap.size()==0){
            System.out.println("无符合条件的结果");
        }else{
            System.out.println(resultMap.size()+"种结果如下:");
        }
        for(Map.Entry<String, Object> entry : resultMap.entrySet()){
            String mapKey = entry.getKey();
            Object mapValue = entry.getValue();
            System.out.println("序号:"+mapKey+"   到期年限总和:"+mapValue);
        }

    }

    public static void removePart(List<Integer> list,Integer num,Map<String, Object> resultMap){
        //定义所有数据二维数组
        int[][] arrayAll = {{2,1},{9,2},{20,3},{12,5},{25,6},{4,7},{15,8},{20,9},{4,10},{9,11},{18,12},{18,15}};
        for(int i = 0 ; i<arrayAll.length ; i++){
            if(!list.contains(arrayAll[i][1])){
                Integer numNew = arrayAll[i][0] + num;
                if(numNew <=(list.size()+1)*10){
                    List<Integer> newList = new ArrayList<>();
                    newList.addAll(list);
                    newList.add(arrayAll[i][1]);
                    Collections.sort(newList);
                    //调用原方法实现递归
                    resultMap.put(newList.toString(),numNew);
                    removePart(newList,numNew,resultMap);
                }
            }
        }
    }
}

运行结果如下
在这里插入图片描述


版权声明:本文为awangb原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。