锁定老帖子 主题:今天面试遇到了雷人面试题求解
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-08-17
这个非常有困难么?还是我考虑的太简单?难点在哪里?我暂时没有看出来
|
|
返回顶楼 | |
发表时间:2010-08-17
最后修改:2010-08-17
先扔个最原始的出来,抛砖引玉
public void testinit(){ List srclist =new ArrayList(8); srclist.add("1"); srclist.add("2"); srclist.add("3"); srclist.add("4"); srclist.add("5"); srclist.add("6"); srclist.add("7"); srclist.add("8"); List targetList = new ArrayList(8); int length = srclist.size(); Random random = new Random(); for (int i = 0, count = length; i < length; i++,count--) { int index =random.nextInt(count); targetList.add(srclist.get(index)); srclist.remove(index); } System.out.println(targetList); } |
|
返回顶楼 | |
发表时间:2010-08-17
这题不雷,实际上是洗牌算法。足够随机的发牌是棋牌游戏的一个基础点。
|
|
返回顶楼 | |
发表时间:2010-08-17
xiaoyuwei 写道 先仍个最原始的出来,抛砖引玉
public void testinit(){ List srclist =new ArrayList(8); srclist.add("1"); srclist.add("2"); srclist.add("3"); srclist.add("4"); srclist.add("5"); srclist.add("6"); srclist.add("7"); srclist.add("8"); List targetList = new ArrayList(8); int length = srclist.size(); Random random = new Random(); for (int i = 0, count = length; i < length; i++,count--) { int index =random.nextInt(count); targetList.add(srclist.get(index)); srclist.remove(index); } System.out.println(targetList); } My LadyGaGa 我试试!! |
|
返回顶楼 | |
发表时间:2010-08-17
xiaoyuwei 写道 先扔个最原始的出来,抛砖引玉
public void testinit(){ List srclist =new ArrayList(8); srclist.add("1"); srclist.add("2"); srclist.add("3"); srclist.add("4"); srclist.add("5"); srclist.add("6"); srclist.add("7"); srclist.add("8"); List targetList = new ArrayList(8); int length = srclist.size(); Random random = new Random(); for (int i = 0, count = length; i < length; i++,count--) { int index =random.nextInt(count); targetList.add(srclist.get(index)); srclist.remove(index); } System.out.println(targetList); } 这个是正解,考虑发给4个人就行了,这么简单的问题哪有那么道道,就是每次随机抽一张牌给一个人就行 |
|
返回顶楼 | |
发表时间:2010-08-17
能用Collections.shuffle()不?
|
|
返回顶楼 | |
发表时间:2010-08-17
这个题应该不算雷人吧?确实看不出哪里雷人了?
|
|
返回顶楼 | |
发表时间:2010-08-17
我记得以前大学课程里面有这个算法题的,这个好像关键是在随机算法上,好像没那么简单的
|
|
返回顶楼 | |
发表时间:2010-08-17
jnduan 写道 能用Collections.shuffle()不?
+1 |
|
返回顶楼 | |
发表时间:2010-08-17
我们可以把上述应用场景模拟为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方法上,取得一个随机数后,从可选扑克牌集合中任意抽取一张,放入新的扑克牌数组中,这样,扑克牌的顺序就打乱了,这时再依次将扑克牌取出来分给玩家,就体现了随机的实现。
|
|
返回顶楼 | |