美文网首页
35取5,12取2

35取5,12取2

作者: 屎倒淋头还嚼便 | 来源:发表于2020-11-02 10:58 被阅读0次
List<Integer> list = new ArrayList<>();
        for(int i=1;i<=35;i++) {
            list.add(i);
        }
        Collections.shuffle(list);
        List<Integer> qian = new ArrayList<>();
        for(int i=1;i<=5;i++) {
            int index =(int)(Math.random()*(35-i)+0); // [0,35-i]
            qian.add(list.remove(index));
            Collections.shuffle(list);
        }
        Collections.sort(qian, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println("前:"+qian);
        // 后
        List<Integer> list2 = new ArrayList<>();
        for(int i=1;i<=12;i++) {
            list2.add(i);
        }
        Collections.shuffle(list2);
        List<Integer> hou = new ArrayList<>();
        for(int i=1;i<=2;i++) {
            int index =(int)(Math.random()*(12-i)+0); // [0,12-i]
            hou.add(list2.remove(index));
            Collections.shuffle(list2);
        }
        Collections.sort(hou, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println("后:"+hou);

相关文章

网友评论

      本文标题:35取5,12取2

      本文链接:https://www.haomeiwen.com/subject/siopvktx.html