把16 支球队随机分为4 个组。

来源:互联网 发布:所谓网络戏谑文化 编辑:程序博客网 时间:2024/05/16 19:50
/*
 24. **(综合)已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。
 注:参赛球队列表见附录
 注2:使用Math.random 来产生随机数。

 2. 2008 北京奥运会男足参赛国家:
 科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利

 思路: A1-4 1-4
 B1-4 5-8
 C1-4 9-12
 D1-4 13-16

 */

import java.util.*;

class Group{
    
     static char groupName='A';
   static void getOneGroup(Set<Integer> set ,Map<Integer ,String> map,Map<Integer,String> groupMap){
    boolean flag=true;
      int count=0,temp=0, tempint=0;
       //随机抽取4个队组成一个组
    while(flag){
      int a= (int)(Math.random()*17);
        for(Iterator<Integer> it =set.iterator();it.hasNext(); )
               {      tempint=it.next();
                    if(tempint==a){ count++;
                               temp=tempint;
                            groupMap.put(count,map.get(tempint));
                        
                        
                        }
                    if(count==4){ flag=false; break;}    
                        
                    }
      
       map.remove(temp);
    }
                 System.out.println("Group"+groupName);     // 显示组别
                 //打印每组的队伍名称
          Set<Map.Entry<Integer, String>> setEntry=groupMap.entrySet();
       Iterator< Map.Entry<Integer,String>>  itEntry  =setEntry.iterator();
       Map.Entry<Integer,String>   mapEntry;
                   while( itEntry.hasNext()){
                                mapEntry =itEntry.next();
                                System.out.println(mapEntry.getKey()+"="+mapEntry.getValue());
                                
                        }
                   
                   groupName=(char)(groupName+1); //下个组别
            }
    
    
    
    }
public class D1 {
    
 
    public static void main(String args[]) {
     
        String teamName[] = { "科特迪瓦", "阿根廷", "澳大利亚", "塞尔维亚", "荷兰", "尼日利亚",
                "日本", "美国", "中国", "新西兰", "巴西", "比利时", "韩国", "喀麦隆", "洪都拉斯",
                "意大利" };

        Map<Integer, String> group = new TreeMap<Integer, String>(); // 将全部球队放入一个容器内抽取
        Map<Integer, String> groupA = new TreeMap<Integer, String>();//A组
        Map<Integer, String> groupB = new TreeMap<Integer, String>();//B组
        Map<Integer, String> groupC = new TreeMap<Integer, String>();//C组
        Map<Integer, String> groupD = new TreeMap<Integer, String>();    //D组
        for (int i = 1; i <= teamName.length; i++)
            group.put(i, teamName[i - 1]);   //所有球队赋给ID
       Set<Integer> set = group.keySet();
       Group  g=   new Group();
       g.getOneGroup(set,group,groupA);//A组抽选
 g.getOneGroup(set,group,groupB);//B组抽选
 g.getOneGroup(set,group,groupC);//C组抽选
 g.getOneGroup(set,group,groupD);//D组抽选
 
}
}
原创粉丝点击