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

/*
 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组抽选
 
}
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值