论坛首页 招聘求职论坛

今天面试遇到了雷人面试题求解

浏览 14153 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-08-17  
llyer110 写道

我们可以把上述应用场景模拟为3个核心对象,玩家(Player),扑克牌(Squeezer)、扑克牌分发引擎(SqueezerDispatchEngine).

 

Player 

 

/**
 * Player
 * @author
 *
 */
public class Player {
	
	/**
	 * squeezers Player catch
	 */
	private List<Squeezer> squeezers;
	
	/**
	 * catch Squeezer and put into list
	 * @param squeezer
	 */
	public void addSqueezer(Squeezer squeezer){
		if(squeezers==null){
			squeezers = new ArrayList<Squeezer>();
		}
		squeezers.add(squeezer);
	}

	/**
	 * return all squeezers of player
	 * @return
	 */
	public List<Squeezer> getSqueezers() {
		return squeezers;
	}

}

 Squeezer.java/**

 * Squeezer
 * @author mit-wh
 *
 */
public class Squeezer {
	
	/**
	 * 花色枚举
	 *
	 */
	public static enum enumflower{blackpetch,redpetch,plumblossom,square,king};
	/**
	 * 扑克牌点子枚举
	 *
	 */
	public static enum enumnumber{two,three,four,five,six,senven,eight,nine,ten,J,Q,K,A,tetrarch,king};
	
	/**
	 * 扑克牌花色
	 */
	private String flower;
	/**
	 * 扑克牌点子
	 */
	private String number;
	
	public Squeezer(String flower,String number){
		this.flower = flower;
		this.number = number;
	}
	
	public String getFlower() {
		return flower;
	}
	public void setFlower(String flower) {
		this.flower = flower;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	
	public String toString(){
		return flower+":"+number;
	}

}

 SqueezerDispatchEngine

 

/**
 * Squeezer dispatch engine,it define the number of player,also can wash squeezer and dispatche to players
 * @author
 *
 */
public class SqueezerDispatchEngine {
	
	//	玩家数量
	private int playernum;
	
	public SqueezerDispatchEngine(int playernum){
		this.playernum = playernum;
	}
	
	/**
	 * 列举所有扑克牌牌面
	 */
	public static Squeezer[] squeeers= new Squeezer[]
	{	new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.two.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.two.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.two.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.two.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.three.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.three.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.three.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.three.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.four.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.four.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.four.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.four.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.five.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.five.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.five.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.five.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.six.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.six.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.six.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.six.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.senven.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.senven.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.senven.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.senven.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.eight.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.eight.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.eight.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.eight.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.nine.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.nine.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.nine.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.nine.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.ten.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.ten.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.ten.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.ten.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.J.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.J.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.J.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.J.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.Q.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.Q.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.Q.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.Q.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.K.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.K.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.K.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.K.toString()),
		new Squeezer(Squeezer.enumflower.blackpetch.toString(),Squeezer.enumnumber.A.toString()),new Squeezer(Squeezer.enumflower.redpetch.toString(),Squeezer.enumnumber.A.toString()),new Squeezer(Squeezer.enumflower.plumblossom.toString(),Squeezer.enumnumber.A.toString()),new Squeezer(Squeezer.enumflower.square.toString(),Squeezer.enumnumber.A.toString()),
		new Squeezer(Squeezer.enumflower.king.toString(),Squeezer.enumnumber.tetrarch.toString()),new Squeezer(Squeezer.enumflower.king.toString(),Squeezer.enumnumber.king.toString())
	};
	
	/**
	 * 洗牌
	 * @param lst
	 * @return
	 */
	public Squeezer[] wash(List<Squeezer> lst)
	{
		Squeezer[] newsqueeer = new Squeezer[54];
		for(int i=newsqueeer.length-1;i>=0;i--){
			int random = (int)(Math.random()*lst.size());
			newsqueeer[i] = lst.get(random);
			lst.remove(random);
		}
		return newsqueeer;
	}
	
	public int getPlayernum(){
		return this.playernum;
	}
	
	/**
	 * 发牌给玩家
	 * @param squeezer
	 * @param player
	 */
	public void dispatche(Squeezer squeezer,Player player){
		player.addSqueezer(squeezer);
	}

}

  TestSqueezer.java(客户端)

 

public class TestSqueezer {

	
	public static void main(String[] args){
		SqueezerDispatchEngine engine = new SqueezerDispatchEngine(4);
		List<Squeezer>lst = new ArrayList<Squeezer>();
		for(int i=0;i<SqueezerDispatchEngine.squeeers.length;i++){
			lst.add(SqueezerDispatchEngine.squeeers[i]);
		}
		Squeezer[] newSqueezer = engine.wash(lst);
		Player[] players = PlayerFactory.createPlayer(engine.getPlayernum());
		for(int i=0,j=0;i<newSqueezer.length;i++,j++){
			Squeezer s =  newSqueezer[i];
			int index = (j*engine.getPlayernum())/engine.getPlayernum();
			Player player = players[index];
			engine.dispatche(s, player);
			if(j==engine.getPlayernum()-1){
				j = -1;
			}
		}
		
		for(int k=0;k<players.length;k++){
			Player player = players[k];
			List<Squeezer> lst1 = player.getSqueezers();
			for(int kk=0;kk<lst1.size();kk++){
				Squeezer s = lst1.get(kk);
				System.out.println("----------------player"+k+"::"+s.toString());
			}
		}
	}
}
   题目中所说的随机两字的实现思想体现在扑克牌分发引擎的wash方法上,取得一个随机数后,从可选扑克牌集合中任意抽取一张,放入新的扑克牌数组中,这样,扑克牌的顺序就打乱了,这时再依次将扑克牌取出来分给玩家,就体现了随机的实现。

 

 

传中的“COPY忍者”?

速度够快 !

技术够好!

 

 

0 请登录后投票
   发表时间:2010-08-17  
曾经笔试的时候遇到过哲学家就餐问题...当时就蒙了.....
0 请登录后投票
   发表时间:2010-08-17  
……话说,先弄一个1..52的array,然后再shuffle一下不就好了??
四人依次取14个
0 请登录后投票
   发表时间:2010-08-18  
等待一次随机算法出现。随机数是很消耗性能的。
0 请登录后投票
   发表时间:2010-08-18  
这题如果是考ruby的就好了,用ruby来实现真是干净爽快:
(1..52).sort_by{rand}.inject([1,[],[],[],[]]){|r,i|r[(r[0]+=1)%4+1]<<i;r}[1..5]


=> [[43, 39, 47, 44, 46, 17, 38, 30, 16, 9, 25, 23, 51], [37, 11, 26, 45, 28, 27
, 35, 29, 41, 15, 5, 20, 21], [18, 7, 13, 42, 36, 31, 22, 2, 12, 3, 32, 8, 1], [
49, 48, 10, 4, 33, 14, 34, 40, 24, 50, 52, 19, 6]]
0 请登录后投票
   发表时间:2010-08-18   最后修改:2010-08-18
hujienihaoya 写道
有52张扑克牌要随机发牌给四个玩家,并且四个玩家牌的数量是相同的?
用java语言写出来

单纯分给4个玩家牌没什么雷的,如果我是负责面试的,可能附加要求发给四个玩家的牌总点数相同
0 请登录后投票
   发表时间:2010-08-18   最后修改:2010-08-18
java core的集合类白学了

Collections.shuffle(list, new Random());

非要把问题复杂化不可
0 请登录后投票
   发表时间:2010-08-18  
1、此需求没有提时间和空间上的限制。
2、没有规定哪种实现方式,只要求用JAVA。

所以,最简单的用系统函数(不知道JAVA有没有提供)

疑问:为什么这么多人对性能这么敏感,为什么非要OO,
      为什么不用最简单的方式(哪怕写一个函数也行),然后需求变化了,再去重构代码
0 请登录后投票
   发表时间:2010-08-18  
这个题也能称为雷题才是真正的雷人。
0 请登录后投票
   发表时间:2010-08-18  
基本上这个题的关键就是发牌要能足够随机。所以就是问个shuffle算法,shuffle算法目前比较好的就是Fisher–Yates算法。Google一下就有。
0 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics