wl549388372
zcolin_
2017-08-23 07:47
采纳率: 100%
浏览 2.1k
已采纳

java实现分组最少重复算法

有这样一个需求:

固定N个元素,分M组(N是M的整数倍), 每个元素只能分进一个组里,且每个组人数相同,保证每轮每组重复的元素最少,输出每次分组明细;

比如 {1,2,3,4,5,6,7,8,9,10,11,12},分四组,分三轮。则结果为

{1,2,3}{4,5,6}{7,8,9}{10,11,12}
{1,4,7}{2,5,10}{3,8,11}{6,9,12}
{1,5,12}{2,4,9}{3,8,10}{6,7,11}

java c++ 都可

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • cao_yanjie
    cao_yanjie 2017-08-24 02:15
    已采纳

    楼主意思是进行五次分组操作,那么可以在原有的基础上,加一个循环,超过五次就跳出循环

     package com.zl.test;
    
    /**
     * 以楼主的例子来说,
     * 先对数组进行随机排序,
     * 然后在拆分随机数组;
     * 将上述情况循环五次操作。
     */
    public class Test4 {
    //  对数组进行随机排列
        public int[] shuffle(int[]arr){
            int len = arr.length;
            for (int i = 0; i < arr.length; i++) {
                int num = (int) Math.floor(Math.random()*(len-i));
                int temp = arr[num];
                arr[num] = arr[len-i-1];
                arr[len-i-1]= temp;
            }
            return arr;
        }
        public static void main(String[] args) {
            int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
            System.out.println("arr数组为:");
            for (int i : arr) {
                System.out.print(i+",");
            }
            Test4 t = new Test4();
    
            int count = 1;
            while(count<6){
    //          对数组进行随机排序
                int[] arrnew = t.shuffle(arr);
    //          对数组进行拆分;
                System.out.println("\n第"+count+"轮");
                int[] a = new int[arrnew.length/4];
                int[] b = new int[arrnew.length/4];
                int[] c = new int[arrnew.length/4];
                int[] d = new int[arrnew.length/4];
                for (int i = 0; i < arrnew.length; i++) {
                    if(i%4 == 0){
                        a[i/4] = arrnew[i];
                    }else if(i%4 == 1){
                        b[i/4] = arrnew[i];
                    }else if(i%4 == 2){
                        c[i/4] = arrnew[i];
                    }else{
                        d[i/4] = arrnew[i];
                    }
                }
    //          对a,b,c,d四个数组进行遍历输出;
                System.out.print("a数组为:");
                for (int i : a) {
                    System.out.print(i+",");
                }
                System.out.print("\nb数组为:");
                for (int i : b) {
                    System.out.print(i+",");
                }
                System.out.print("\nc数组为:");
                for (int i : c) {
                    System.out.print(i+",");
                }
                System.out.print("\nd数组为:");
                for (int i : d) {
                    System.out.print(i+",");
                }
                count++;
            }
        }
    }
    
    

    运行结果

    点赞 评论
  • cao_yanjie
    cao_yanjie 2017-08-23 10:34

    package com.zl.test;
    /**

    • 以楼主的例子来说,
    • 先对数组进行随机排序,
    • 然后在拆分随机数组; * / public class Test4 { // 对数组进行随机排列 public int[] shuffle(int[]arr){ int len = arr.length; for (int i = 0; i < arr.length; i++) { int num = (int) Math.floor(Math.random()(len-i)); int temp = arr[num]; arr[num] = arr[len-i-1]; arr[len-i-1]= temp; } return arr; } public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12}; System.out.println("arr数组为:"); for (int i : arr) { System.out.print(i+","); } Test4 t = new Test4(); int[] arrnew = t.shuffle(arr); // 输出随机排列后的数组,测试用 System.out.println("\n随机排序后的arrnew数组为:"); for (int i : arrnew) { System.out.print(i+","); } // 对数组进行拆分; int[] a = new int[arrnew.length/4]; int[] b = new int[arrnew.length/4]; int[] c = new int[arrnew.length/4]; int[] d = new int[arrnew.length/4]; for (int i = 0; i < arrnew.length; i++) { if(i%4 == 0){ a[i/4] = arrnew[i]; }else if(i%4 == 1){ b[i/4] = arrnew[i]; }else if(i%4 == 2){ c[i/4] = arrnew[i]; }else{ d[i/4] = arrnew[i]; } } // 对a,b,c,d四个数组进行遍历输出; System.out.println("\na数组为:"); for (int i : a) { System.out.print(i+","); } System.out.println("\nb数组为:"); for (int i : b) { System.out.print(i+","); } System.out.println("\nc数组为:"); for (int i : c) { System.out.print(i+","); } System.out.println("\nd数组为:"); for (int i : d) { System.out.print(i+","); } } }
    点赞 评论
  • cao_yanjie
    cao_yanjie 2017-08-23 10:36

    以上为例,先对数组进行随机排序,然后在拆分数组,以下为代码

     package com.zl.test;
    
    /**
     * 以楼主的例子来说,
     * 先对数组进行随机排序,
     * 然后在拆分随机数组;
     *
     */
    public class Test4 {
    //  对数组进行随机排列
        public int[] shuffle(int[]arr){
            int len = arr.length;
            for (int i = 0; i < arr.length; i++) {
                int num = (int) Math.floor(Math.random()*(len-i));
                int temp = arr[num];
                arr[num] = arr[len-i-1];
                arr[len-i-1]= temp;
            }
            return arr;
        }
        public static void main(String[] args) {
            int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
            System.out.println("arr数组为:");
            for (int i : arr) {
                System.out.print(i+",");
            }
            Test4 t = new Test4();
            int[] arrnew = t.shuffle(arr);
    //      输出随机排列后的数组,测试用
            System.out.println("\n随机排序后的arrnew数组为:");
            for (int i : arrnew) {
                System.out.print(i+",");
            }
    //      对数组进行拆分;
            int[] a = new int[arrnew.length/4];
            int[] b = new int[arrnew.length/4];
            int[] c = new int[arrnew.length/4];
            int[] d = new int[arrnew.length/4];
            for (int i = 0; i < arrnew.length; i++) {
                if(i%4 == 0){
                    a[i/4] = arrnew[i];
                }else if(i%4 == 1){
                    b[i/4] = arrnew[i];
                }else if(i%4 == 2){
                    c[i/4] = arrnew[i];
                }else{
                    d[i/4] = arrnew[i];
                }
            }
    //      对a,b,c,d四个数组进行遍历输出;
            System.out.println("\na数组为:");
            for (int i : a) {
                System.out.print(i+",");
            }
            System.out.println("\nb数组为:");
            for (int i : b) {
                System.out.print(i+",");
            }
            System.out.println("\nc数组为:");
            for (int i : c) {
                System.out.print(i+",");
            }
            System.out.println("\nd数组为:");
            for (int i : d) {
                System.out.print(i+",");
            }
        }
    }
    
    
    点赞 评论
  • cao_yanjie
    cao_yanjie 2017-08-23 10:38

    以上为例,先对数组进行随机排序,然后在拆分数组,以下为代码图片说明

     package com.zl.test;
    
    /**
     * 以楼主的例子来说,
     * 先对数组进行随机排序,
     * 然后在拆分随机数组;
     *
     */
    public class Test4 {
    //  对数组进行随机排列
        public int[] shuffle(int[]arr){
            int len = arr.length;
            for (int i = 0; i < arr.length; i++) {
                int num = (int) Math.floor(Math.random()*(len-i));
                int temp = arr[num];
                arr[num] = arr[len-i-1];
                arr[len-i-1]= temp;
            }
            return arr;
        }
        public static void main(String[] args) {
            int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
            System.out.println("arr数组为:");
            for (int i : arr) {
                System.out.print(i+",");
            }
            Test4 t = new Test4();
            int[] arrnew = t.shuffle(arr);
    //      输出随机排列后的数组,测试用
            System.out.println("\n随机排序后的arrnew数组为:");
            for (int i : arrnew) {
                System.out.print(i+",");
            }
    //      对数组进行拆分;
            int[] a = new int[arrnew.length/4];
            int[] b = new int[arrnew.length/4];
            int[] c = new int[arrnew.length/4];
            int[] d = new int[arrnew.length/4];
            for (int i = 0; i < arrnew.length; i++) {
                if(i%4 == 0){
                    a[i/4] = arrnew[i];
                }else if(i%4 == 1){
                    b[i/4] = arrnew[i];
                }else if(i%4 == 2){
                    c[i/4] = arrnew[i];
                }else{
                    d[i/4] = arrnew[i];
                }
            }
    //      对a,b,c,d四个数组进行遍历输出;
            System.out.println("\na数组为:");
            for (int i : a) {
                System.out.print(i+",");
            }
            System.out.println("\nb数组为:");
            for (int i : b) {
                System.out.print(i+",");
            }
            System.out.println("\nc数组为:");
            for (int i : c) {
                System.out.print(i+",");
            }
            System.out.println("\nd数组为:");
            for (int i : d) {
                System.out.print(i+",");
            }
        }
    }
    
    
    点赞 评论
  • u012533102
    SevAS 2017-08-23 12:14

    分组可利用N%M==0可分组则继续

    点赞 评论

相关推荐