锁定老帖子 主题:今天面试遇到了雷人面试题求解
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-08-18
rainchen 写道 这题如果是考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]] |
|
返回顶楼 | |
发表时间:2010-08-18
wanglong1615 写道 四个线程并发访问五十二张扑克牌.
线程退出条件:次数i<52/4 有问题吧,并发访问可能会出现有人多牌,有人少牌的情况。 |
|
返回顶楼 | |
发表时间:2010-08-18
不雷人呀,感觉很简单,没什么亮点的题。。
|
|
返回顶楼 | |
发表时间:2010-08-18
public static void main(String[] args) { String[] suits = { "方片", "梅花", "红桃", "黑桃" }; String[] cardValues = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; int cardsInDeck = 52; Vector<String> deck = new Vector<String>(cardsInDeck); LinkedList<String> shuffledDeck = new LinkedList<String>(); Random chooser = new Random(); // Card chooser // Load the deck for (String suit : suits) { for (String cardValue : cardValues) { deck.add(suit+cardValue); } } // Select cards at random from the deck to transfer to shuffled deck int selection = 0; // Selected card index for (int i = 0; i < cardsInDeck; i++) { selection = chooser.nextInt(deck.size()); shuffledDeck.add(deck.remove(selection)); } // Deal the cards from the shuffled deck into four hands StringBuffer[] hands = { new StringBuffer("Hand 1:"), new StringBuffer("Hand 2:"), new StringBuffer("Hand 3:"), new StringBuffer("Hand 4:") }; ListIterator cards = shuffledDeck.listIterator(); while (cards.hasNext()) { for (StringBuffer hand : hands) { hand.append(' ').append((String) (cards.next())); } } // Display the hands for (StringBuffer hand : hands) { System.out.println(hand.toString()); } } |
|
返回顶楼 | |
发表时间:2010-08-18
生成随机数算法吧~~
|
|
返回顶楼 | |
发表时间:2010-08-18
wanglong1615 写道 四个线程并发访问五十二张扑克牌.
线程退出条件:次数i<52/4 不错的想法 ....支持 |
|
返回顶楼 | |
发表时间:2010-08-18
模拟下现实的场景是先买来一副牌,然后搓牌,然后轮流一张张发给4个人,反正已经搓过了,所以每人一口气拿13张效果也是随机的。
public static void main(String[] args) { List<Long> list = new ArrayList<Long>(); // 生成52张牌 for (Long i = 1L; i < 53L; i++) { list.add(i); } // 把牌打乱 Collections.shuffle(list); // 给4个人发牌,52/4=13 ,每13张牌就换行输出 for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)); if ((i + 1) % 13 == 0) { System.out.println(); } else { System.out.print(","); } } } } |
|
返回顶楼 | |
发表时间:2010-08-18
最后修改:2010-08-18
|
|
返回顶楼 | |
发表时间:2010-08-18
sdh5724 写道 等待一次随机算法出现。随机数是很消耗性能的。
最重要的是伪随机数有可能会产生胜率偏向 |
|
返回顶楼 | |
发表时间:2010-08-18
写得很简单:
public class Test { public static void main(String[] args) throws InterruptedException { Cards cards = new Cards(); Person person1 = new Person(cards,"persion1"); Person person2 = new Person(cards,"persion2"); Person person3 = new Person(cards,"persion3"); Person person4 = new Person(cards,"persion4"); person1.start(); person2.start(); person3.start(); person4.start(); person1.join(); person2.join(); person3.join(); person4.join(); person1.show(); person2.show(); person3.show(); person4.show(); } } class Cards{ int[][] cards = new int[4][13]; int color = 0; int count = 0; protected Cards() { initialize(); } protected void initialize(){ for (int i=0; i < 4; i++){ for (int j=1; j <= 13; j++){ cards[i][j-1] = j; } } } synchronized int getCard(){ if (color >= 4){ throw new IllegalStateException("No card."); } int car = cards[color][count]; count ++; if (count >=13){ color ++; count = 0; } return car; } } class Person extends Thread{ int[] personalCards = new int[13]; int count = 0; Cards cards =null; Person (Cards argCards, String argThreadName){ super(argThreadName); cards = argCards; } /** {@inheritDoc} */ @Override public void run() { while (count < 13){ personalCards[count] = cards.getCard(); count++; } } public void show() { System.out.println(); System.out.println(this.getName()+":"); for (int i : personalCards){ System.out.print(i + ", "); } } } 运行结果: persion1: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, persion2: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, persion3: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, persion4: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, |
|
返回顶楼 | |