84452抽签Java版

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Test {


    public static void main(String[] args) {
        Map<Integer, Map<Integer,String>> map = new HashMap();

        Map<Integer,String> A = new HashMap<>();
        Map<Integer,String> B = new HashMap<>();
        Map<Integer,String> C = new HashMap<>();
        Map<Integer,String> D = new HashMap<>();

        A.put(0,"paris");
        A.put(1,"crystal");
        B.put(0,"milan");
        B.put(1,"porto");
        C.put(0,"forest");
        C.put(1,"chelsea");
        D.put(0,"mancity");
        D.put(1,"barcelona");

        map.put(0,A);
        map.put(1,B);
        map.put(2,C);
        map.put(3,D);

        Random random = new Random();
//        System.out.println(random.nextInt(4));
//        System.out.println(random.nextInt(4));
//        System.out.println(random.nextInt(4));
//        System.out.println(random.nextInt(4));
//        System.out.println(random.nextInt(4));

        int i = 100;
        int j = 100;
        Map m = null;
        int count = 0;

        int group = 4;   //记录组信息

        /**
         * 这个抽签是先抽小组,再抽球队(这步可以直接按名次来),同组使用group字段进行分别,只用了基础map结构,js应该可改造
         */

        //js可以用下面这个判断key是否存在
        //ary.hasOwnProperty(key); 或 obj.hasOwnProperty(key);
        while (count < 8) {
            i = random.nextInt(1000) % 4;  // 先抽取一个小组
            if(i == group) {  //如果同组重新抽
                continue;
            }
            if(map.containsKey(i)){  //判断一下是否已经被抽取,防止异常
                m = map.get(i);
                if (count % 2 == 0 || count == 0){  // 判断是否需要抽球队,这个也可以不用,直接按照先抽第一再抽第二就行了
                    j = random.nextInt(1000) % 2;
                }else {
                    if (j == 1){
                        j--;
                    }else {
                        j++;
                    }
                }
                if(m.containsKey(j)){
                    count++;
                    System.out.println("第" + count + "个队伍是:" + m.get(j));
                    if (count % 2 != 0) {
                        group = i;
                    }else {
                        group = 4;
                    }
                    map.get(i).remove(j);  //抽完排除掉
                }
            }
        }

    }
}

上一篇:314. Binary Tree Vertical Order Traversal


下一篇:从大学混子,到国企20K的越级选手,我的python面试经历你想看吗(可免费下载)