Topic: 一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) |
Print this page |
1.一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) | Copy to clipboard |
Posted by: toupeen Posted on: 2006-12-13 17:40 1.给定一个数组,要求把扑克按照"黑桃""红桃""方块""梅花"的顺序排列,每种花型又要按照"A,2,3,4,5,6,7,8,9,10,J,Q,K"的顺序排好 2.模仿洗牌,把数组里的牌随意打乱. 这是小弟应聘时的一道笔试题,没搞定,向大家请教下!!谢谢!!! |
2.Re:一道面试题,向大家请教!(在线等) [Re: toupeen] | Copy to clipboard |
Posted by: luozhe0107 Posted on: 2006-12-15 10:47 随便怎么都可以解决,最简单的是定义一个 char arr=['a','2','3',.......] for( i=0;i<100;i++) { a=(int)(random(1)*54); b=(int)(random(1)*54); swap(a,b) } 即OK |
3.Re:一道面试题,向大家请教!(在线等) [Re: toupeen] | Copy to clipboard |
Posted by: ycxct Posted on: 2006-12-19 22:25
|
4.Re:一道面试题,向大家请教!(在线等) [Re: toupeen] | Copy to clipboard |
Posted by: luozhe0107 Posted on: 2006-12-22 12:27 楼上的写的好,受益,TKS, |
5.Re:一道面试题,向大家请教!(在线等) [Re: luozhe0107] | Copy to clipboard |
Posted by: dreamsky2 Posted on: 2006-12-22 14:22 public int compareTo(card cobj) { return color * 10 + point - (cobj.color * 10 + cobj.point); } 10要改为大于12的数才能正确排序 |
6.Re:一道面试题,向大家请教!(在线等) [Re: toupeen] | Copy to clipboard |
Posted by: ycxct Posted on: 2006-12-22 17:29 汗,谢谢指正,我记忆中应该写的是100...马虎了 |
7.Re:楼上的太烦了,搞个简单的 [Re: toupeen] | Copy to clipboard |
Posted by: liushuiboy Posted on: 2007-01-24 12:41 public enum EnumPuke { heiTao,hongTao,fangkuai,meiHua; String[]point={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; } import java.util.*; public class AcessPuke { public static void main(String[] args) { // TODO Auto-generated method stub List<String> list = new ArrayList<String>(); String[] colors = { "黑桃", "红桃", "方块", "梅花" }; for (EnumPuke puke : EnumPuke.values()) { for (String str : puke.point) { list.add(colors[puke.ordinal()] + str); } } System.out.println(list); Collections.shuffle(list); System.out.println(list); } } |
8.Re:一道面试题,向大家请教!(在线等) [Re: toupeen] | Copy to clipboard |
Posted by: java658 Posted on: 2007-01-26 10:32 不是太懂 |
9.Re:一道面试题,向大家请教!(在线等) [Re: toupeen] | Copy to clipboard |
Posted by: ken_chie Posted on: 2007-01-26 12:52 3楼写得第一行 为什么可以那样定义一个类 能否给说明一下 小弟初学者不明白 为什么 轻指点 谢谢 |
10.Re:一道面试题,向大家请教!(在线等) [Re: ken_chie] | Copy to clipboard |
Posted by: xiaofengtoo Posted on: 2007-02-19 13:35 ken_chie wrote: 好像是JDK1。5后面引用的范行。 |
11.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: sljliuan001 Posted on: 2007-02-24 20:53 import java.awt.*; import java.applet.Applet; public class DeckOfCards extends Applet{ private Card deck[]; private int currentCard; private Button dealButton, shuffleButton; private TextField displayCard; public void init(){ String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; deck = new Card[ 52 ]; currentCard = -1; for( int i = 0; i < deck.length; i++ ) deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] ); dealButton = new Button( "Deal card" ); shuffleButton = new Button( "shuffle cards" ); displayCard = new TextField( 20 ); displayCard.setEditable( false ); add( dealButton ); add( shuffleButton ); add( displayCard ); } public boolean action( Event event, Object object ){ if( event.target == dealButton ){ Card dealt = dealCard(); if( dealt != null ){ displayCard.setText( dealt.toString() ); showStatus( "Card #: " + currentCard ); } else { displayCard.setText( "NO MORE CARDS TO DEAL!" ); showStatus( "Shuffle cards to continue!" ); } } else if( event.target == shuffleButton ){ displayCard.setText( "SHUFFLE CARDS...." ); showStatus( "" ); shuffle(); displayCard.setText( "DECK IS SHUFFLED!" ); } return true; } public void shuffle(){ currentCard = -1; for( int i = 0; i < deck.length; i++ ){ int j = ( int )( Math.random() * 52 ); Card temp = deck[ i ]; deck[ i ] = deck[ j ]; deck[ j ] = temp; } dealButton.enable(); } public Card dealCard(){ if( ++currentCard < deck.length ) return deck[ currentCard ]; else { dealButton.disable(); return null; } } } class Card{ private String face; private String suit; public Card( String f, String s ){ face = f; suit = s; } public String toString(){ return face + " of " + suit ; } } |
12.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: dorrenchen Posted on: 2007-03-19 14:09 in ruby
|
13.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: renchao775 Posted on: 2007-03-20 09:53 楼上的写的是JAVA么? |
14.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: aaarong Posted on: 2007-03-21 20:49 public enum EnumPuke { heiTao,hongTao,fangkuai,meiHua; String[]point={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; } 声明的是枚举类... 同时有个疑问: colors[puke.ordinal()] 这句中的puke.ordinal() 方法实现的是什么功能? |
15.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: zip2007 Posted on: 2007-03-27 20:33 你们太强了 |
16.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: xuxiaolei Posted on: 2007-03-28 19:49 String[] type = {"黑桃", "红桃", "方块", "梅花"}; String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; StringBuffer[] seqArray = new StringBuffer[type.length * num.length]; StringBuffer[] randomArray = new StringBuffer[seqArray.length]; int index = 0; //初始化seqArray数组 for(int i = 0; i < type.length; i++) { for(int j = 0; j < num.length; j++) { seqArray[index] = new StringBuffer(); seqArray[index].append(type[i]); seqArray[index].append(" "); seqArray[index].append(num[j]); index++; } } int j = 0; for(int i = randomArray.length; i > 0; i--) { index = (int)(Math.random() * i); randomArray[j++] = seqArray[index]; seqArray[index] = seqArray[i - 1]; } for(int i = 0; i < randomArray.length; i++) { System.out.println(randomArray[i]); } |
17.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: xuxiaolei Posted on: 2007-03-29 07:55 3楼的ycxct写的这几条语句非常好,比我写的好多了 for (int i = 0; i < 52; i++) { pk.add(new card((i/13)%4,i%13)); } ArrayList<card> pk=new ArrayList<card>(); for (int i = 0; i < 52; i++) { pk.add(new card((i/13)%4,i%13)); } System.out.println(pk); Collections.shuffle(pk); //洗牌 System.out.println(pk); |
18.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: wxnis1 Posted on: 2007-04-01 21:49 你们真的是太强拉,随便一到看似简单的问题 但 写出的程序 是这样的复杂 和经典 佩服 ,小第 现在正在自学java 我相信我有一天也会 达到这样的境界的 |
19.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: suntao19830709 Posted on: 2007-04-28 13:29 面试人家就是要看你的写程序的能力, 如果最关键的排序和打乱的算法都要用java自带的东西,人家怎么考察你的水平??? 人家考你的就是sort和shuffle,而楼上的很多解答却在关键的算法上含糊,直接调用原有的代码.那人家还考你干嘛? 审不了题,已然输了~ |
20.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: xiezhuojun2006 Posted on: 2007-05-05 23:31 都看不懂呢,怎么办? |
21.Re:一道面试题,向大家请教!(在线等) [Re: dreamsky2] | Copy to clipboard |
Posted by: liangzi232004 Posted on: 2007-06-21 16:19 dreamsky2 wrote: 请问这两句是什么含义,并且为什么大于12才能正确排序呢? |
22.Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re: toupeen] | Copy to clipboard |
Posted by: andy_wang_5 Posted on: 2007-06-22 14:36 听suntao19830709的建议,把ycxct的程序该了一下。 import java.util.*; class comp implements Comparator<card> { public int compare(card card01, card card02) { int i = (int)(Math.random()*2); if (i == 0) return -1; return i; } } class card implements Comparable<card> { int color; //1-4 int point; //1-13 card(int color, int point) { this.color = color; this.point = point; } public int compareTo(card cobj) { return color * 12 + point - (cobj.color * 12 + cobj.point); } public String toString() { String str=null; switch (color) { case 0: str = "桃"; break; case 1: str = "心"; break; case 2: str = "梅"; break; case 3: str = "方"; break; } switch(point) { case 0: str += " A"; break; case 10: str += " J"; break; case 11: str += " Q"; break; case 12: str += " K"; break; default: str+=" "+ (point+1); } return str; } } public class test2 { public static void main(String[] args) { ArrayList<card> pk=new ArrayList<card>(); for (int i = 0; i < 52; i++) { pk.add(new card((i/13)%4,i%13)); } //System.out.println(pk); //Collections.shuffle(pk); //洗牌 Collections.sort(pk, new comp()); Collections.sort(pk, new comp()); Collections.sort(pk, new comp()); System.out.println(pk); Collections.sort(pk); //排序 System.out.println(pk); } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |