面试遇到了雷人面试题求解_续集

有52张扑克牌要随机发牌给四个玩家,并且四个玩家牌的数量是相同的? 用java语言写出来 http://www.iteye.com/topic/739333

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

import org.junit.Test;

/**
 * k代表红桃,黑桃,梅花,方片
 * @author Administrator
 *
 */
public class TestJavaEye {

	Map<String, Object> mp = new HashMap<String, Object>();
    int a = 0,b=1,c=2,d=3; //A,B,C,D  对应每一个人的牌
    int t = 1; 
	
	@Test
	public  void testinit(){ 
        //创建牌	
      	for(int w = 0;w<52;w++){
			if(w<13){
				
				mp.put(w+"","红桃:"+(13-w));
				//System.out.println(w+"<坐标......值>"+mp.get(w+""));
			}else
			if(w<26){			
				mp.put(w+"","黑桃"+(26-w));
				//System.out.println(w+"<坐标......值>"+mp.get(w+""));
			}else
			if(w<39){
				mp.put(w+"","梅花"+(39-w));
				//System.out.println(w+"<坐标......值>"+mp.get(w+""));
			}else{
				mp.put(w+"","方片"+(52-w));
				//System.out.println(w+"<坐标......值>"+mp.get(w+""));
			}
			}
        int length = mp.size(); 
        Random random = new Random(); 
        /**
         * 开始发牌
         */
        for (int i = 0; i < length; i++) 
        {
             	//0123
        		//52/4
            	//4567
            	if((i)%4==0){
            		System.out.println("----------------第"+t+"次发牌-----------------");
            		t++;
            	}
                int index =random.nextInt(length);    
                for(;mp.get(index+"")==null;){
                	index = random.nextInt(length);
                }
                if(i == a){
                	 System.out.println(a+"A拥有牌索引值:"+index+",牌值:"+mp.get(index+""));   
                 	a = a+4; 
                }
                if(i == b){
                	 System.out.println(b+"B拥有牌索引值:"+index+",牌值:"+mp.get(index+""));   
                 	b = b+4; 
                } 
                if(i == c){
                	
                	 System.out.println(c+"C拥有牌索引值:"+index+",牌值:"+mp.get(index+"")); 
                	 c = c+4; 
                } 
                if(i == d){
                	
                	 System.out.println(d+"D拥有牌索引值:"+index+",牌值:"+mp.get(index+""));  
                	 d = d+4; 
                } 
                mp.remove(index+""); 
                
        }
    }
	
}

 

 

你可能感兴趣的:(C++,c,面试,C#,JUnit)