用java编写一个发扑克牌的程序

qingqingcao22009 2012-10-26 06:31:26
发扑克牌:有52张扑克牌(从1到52),要随机分发到数组Card[52]中,请设计程序实现。
这是今天做的一个笔试题。
我自己能想到的就是用Random生成随机数,然后通过求余算法生成数字,放入Set集合中,知道集合中的元素个数为52,停止生成,再把Set中的数据放入数组中。

这样性能是不是不太好?具体随机数的生成怎么实现?

我想作为笔试题最后一题,应该考察的方面挺多的。

听大家的意见……
...全文
4119 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaerfeng 2014-05-03
  • 打赏
  • 举报
回复
可以参考代码:Java 扑克发牌算法实现,下载地址:http://www.zuidaima.com/share/1550463274224640.htm
_jerrytiger 2012-10-27
  • 打赏
  • 举报
回复

public static void main(String[] args) {
Random r = new Random();
LinkedList<Integer> array = new LinkedList<Integer>();
for (int i = 0; i < 52; i++) {
array.add(i);
}

int puke[] = new int[52];

int i = 0 ;
while(array.size() > 0){
int index = r.nextInt(array.size());
puke[i++] = array.remove(index);
}

System.out.println(Arrays.toString(puke));
}
_jerrytiger 2012-10-27
  • 打赏
  • 举报
回复

用链表保存最开始的数字,比较合适,因为移除对象效率很高。


设计链表保存 1 - 52 之间的数字 。
LinkList pukeList ;

第一步:实例化一个 int[52] 的数组

第二步:
int index = 0 ;
whlie(pukeList.length > 0 ){
int i = Random.nextInt(pukeList.length); // 去数组长度里面的随机数
int[index++] = pukeList.remove(i);
}
qingqingcao22009 2012-10-27
  • 打赏
  • 举报
回复
这个听别人说,不行。因为set虽然无序,但不能模拟随机过程。因为它的原理是hashCode(),hash表虽然无序,但是它是经过一个运算的。

别人这么说的,不明白啊[Quote=引用 3 楼 的回复:]
直接在set里面放入1到52,让后迭代他们就行了 ,因为set是无序的,每次得到的都不同
[/Quote]
张某码 2012-10-27
  • 打赏
  • 举报
回复
用他自带的一个洗牌函数很快就能实现分牌的随机性Collections.shuffle(),就不要用随机数了。
qingqingcao22009 2012-10-27
  • 打赏
  • 举报
回复
我写了两个程序,第一个跟你这个是一样的。这个更好点。不用考虑花色的。[Quote=引用 12 楼 的回复:]
如果不考虑的话 看下这个

Java code
public class RandomPuke {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i=1;i<=52;i++){
list.add(……
[/Quote]
zly361814478 2012-10-27
  • 打赏
  • 举报
回复
如果不考虑的话 看下这个
public class RandomPuke {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i=1;i<=52;i++){
list.add(i); // 添加到数组列表去。
}
Collections.shuffle(list); // 让列表内容随机重组
int[] card =new int[52];
for(int s=0;s<list.size();s++){
card[s]=list.get(s); // 放到数组里
}
System.out.println(Arrays.toString(card)); //打印出来
}
}
zly361814478 2012-10-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

这个是用Ramdom编写的,性能应该不太好
Java code
import java.util.Random;
public class CartDemo1 {
public static void main(String[] args) {
int[] cards = new CartDemo1().getCards();
for (int i :……
[/Quote] 不用考虑 扑克牌的花色?
qingqingcao22009 2012-10-26
  • 打赏
  • 举报
回复
这个是用Ramdom编写的,性能应该不太好
import java.util.Random;
public class CartDemo1 {
public static void main(String[] args) {
int[] cards = new CartDemo1().getCards();
for (int i : cards) {
System.out.print(i+" ");
}
}
public int[] getCards(){
boolean[] isGet = new boolean[52];
int[] cards = new int[52];
Random r = new Random();
int index = 0;
while(true){
int i = r.nextInt(52);
if(isGet[i]){
continue;
}
isGet[i] = true;
cards[index++] = i+1;
if(index == 52){
break;
}
}
return cards;
}
}
金墨痴 2012-10-26
  • 打赏
  • 举报
回复
好复杂。。。
qingqingcao22009 2012-10-26
  • 打赏
  • 举报
回复
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class CartDemo {
public static void main(String[] args) {
int[] cards = new CartDemo().getCards();
for (int i : cards) {
System.out.print(i+" ");
}
}
public int[] getCards(){
List<Integer> list = new LinkedList<Integer>();
int index = 1;
for (int i = 0; i < 52; i++) {
list.add(index++);
}
Collections.shuffle(list);
int[] cards = new int[52];
for (int i = 0; i < cards.length; i++) {
cards[i] = list.remove(0);
}
return cards;
}
}

这个是别人帮我写的一个,实现的很简单,代码简洁清晰
aleijie 2012-10-26
  • 打赏
  • 举报
回复
直接在set里面放入1到52,让后迭代他们就行了 ,因为set是无序的,每次得到的都不同
wangdong20 2012-10-26
  • 打赏
  • 举报
回复
从card[0]开始扁历,把当前的card 与card[rand.nextInt(51)]交换,遍历一次后,也就随机得差不多了
kilimanjaroup 2012-10-26
  • 打赏
  • 举报
回复
一个set直接存放51张卡,每次用Random随机取出来一个?

好像作为最后一题是有点简单了...

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧