java:洗牌,

发布于 2024-10-20 14:34:00 字数 475 浏览 1 评论 0原文

这是一个面试问题。请给一些提示:

用向量实现一个方法,洗一副牌。

public class Card {
    private int value;
    Card(int v) {
        value = v;
        }

    public void print(){
        System.out.print(value+";");
    }
}



public class DeckShuffle {

    private final int num;
    Vector<Card> deck = new Vector<Card>();

// implement this shuffle function. DO NOT USE Collections.shuffle() !!
public void shuffle(){
// your code goes here!
}



}

This is a interview-questions. Please give some hints:

Use vector to implement a method, shuffle a deck of Card.

public class Card {
    private int value;
    Card(int v) {
        value = v;
        }

    public void print(){
        System.out.print(value+";");
    }
}



public class DeckShuffle {

    private final int num;
    Vector<Card> deck = new Vector<Card>();

// implement this shuffle function. DO NOT USE Collections.shuffle() !!
public void shuffle(){
// your code goes here!
}



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

信仰 2024-10-27 14:34:00

可以在 JDK 或 OpenJDK 的源包中找到,但算法非常简单(对于集合和数组来说基本上是相同的):

given a[]
for i <- 0..a.length-2
  rnd_index <- random(i, a.length) #inclusive, exclusive
  swap a[i] and a[rnd_index]
next

Collections.shuffle() 的代码 就位,因此您不需要额外的并行内存。它被称为Fisher Yates shuffle

The code for Collections.shuffle() can be found in the source bundle for the JDK or from OpenJDK, but the algorithm is pretty simple (basically the same for a collection as for an array):

given a[]
for i <- 0..a.length-2
  rnd_index <- random(i, a.length) #inclusive, exclusive
  swap a[i] and a[rnd_index]
next

This works in place so you don't need extra parallel memory. It is known as the Fisher Yates shuffle.

眼藏柔 2024-10-27 14:34:00

我想到的是:

public void shuffle() {
    Vector<Card> v = new Vector<Card>(deck.size());
    int size = deck.size();
    for(int i = 0; i < size; i++) {
        int index = Math.random() * deck.size();
        Card c = deck.remove(index);
        v.add(c);
    }
    deck = v;
}

这是干编码,没有进行测试。

Here's what comes to mind :

public void shuffle() {
    Vector<Card> v = new Vector<Card>(deck.size());
    int size = deck.size();
    for(int i = 0; i < size; i++) {
        int index = Math.random() * deck.size();
        Card c = deck.remove(index);
        v.add(c);
    }
    deck = v;
}

This is dry coded, no testing done.

浮华 2024-10-27 14:34:00
void Shuffle()
{
  int n= deck.size();
  Vector<Card> v = new Vector<Card>(n);


  for (int i = 0; i < n; i++) {
     int j = (int)((i-1) * Math.random() )+ 1;
     if ( i != j ) {
         swap(cards, i, j);
     }
  }

  deck = v;
}
void Shuffle()
{
  int n= deck.size();
  Vector<Card> v = new Vector<Card>(n);


  for (int i = 0; i < n; i++) {
     int j = (int)((i-1) * Math.random() )+ 1;
     if ( i != j ) {
         swap(cards, i, j);
     }
  }

  deck = v;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文