r*d
3 楼
不知道标准的shuffle card是什么算法
我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
面一半和后面一般间隔混起来?
void fun()
{
.....
for ( int i =1; i {
card_after[i]=card_before[i];
card_after[i+1]=card_before[i+n/2];
}
}
我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
面一半和后面一般间隔混起来?
void fun()
{
.....
for ( int i =1; i
card_after[i]=card_before[i];
card_after[i+1]=card_before[i+n/2];
}
}
I*s
4 楼
http://en.wikipedia.org/wiki/Shuffling
"Shuffling algorithms
In a computer, shuffling is equivalent to generating a random permutation of
the cards. There are two basic
algorithms for doing this, both popularized by Donald Knuth.
The first is simply to assign a random number to each card, and then to sort
the cards in order of their random
numbers. This will generate a random permutation, unless any of the random
numbers generated are the
same as any others (i.e. pairs, triplets etc). This can be eliminated either
assigning new random numbers to
these cases, or reduced to an arbitrarily low probability by choosing a
sufficiently wide range of random
number choices. If using efficient sorting such as mergesort or heapsort,
this is an O(n log n) algorithm.
The second, generally known as the Knuth shuffle or Fisher–Yates shuffle,
is a linear-time algorithm which
involves moving through the pack from top to bottom, swapping each card in
turn with another card from a
random position in the part of the pack that has not yet been passed through
(including itself). Providing that
the random numbers are unbiased, this will always generate a random
permutation."
【在 w*****3 的大作中提到】
: java
: 不准用
: math.random()
"Shuffling algorithms
In a computer, shuffling is equivalent to generating a random permutation of
the cards. There are two basic
algorithms for doing this, both popularized by Donald Knuth.
The first is simply to assign a random number to each card, and then to sort
the cards in order of their random
numbers. This will generate a random permutation, unless any of the random
numbers generated are the
same as any others (i.e. pairs, triplets etc). This can be eliminated either
assigning new random numbers to
these cases, or reduced to an arbitrarily low probability by choosing a
sufficiently wide range of random
number choices. If using efficient sorting such as mergesort or heapsort,
this is an O(n log n) algorithm.
The second, generally known as the Knuth shuffle or Fisher–Yates shuffle,
is a linear-time algorithm which
involves moving through the pack from top to bottom, swapping each card in
turn with another card from a
random position in the part of the pack that has not yet been passed through
(including itself). Providing that
the random numbers are unbiased, this will always generate a random
permutation."
【在 w*****3 的大作中提到】
: java
: 不准用
: math.random()
w*3
5 楼
这题是MS的onsite题,
我觉得random是必须的吧,
难道这题是压力测试?
我觉得random是必须的吧,
难道这题是压力测试?
E*a
6 楼
you are talking about perfect shuffling, like A1A2A3B1B2B3 A1B1A2B2A3B3
I guess lz talked about random shuffling
【在 r******d 的大作中提到】
: 不知道标准的shuffle card是什么算法
: 我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
: 那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
: 面一半和后面一般间隔混起来?
: void fun()
: {
: .....
: for ( int i =1; i
: card_after[i]=card_before[i];
相关阅读
interview的书。在美国renew的护照 issue country 算哪呢免费$500或2次往返机票offer Sapphire Preferred Card求facebook的推荐,我的resume看起来还好,不丢你脸。呵呵。请问OPT找学校老师挂靠的问题,多谢!跟面试的打电话直打磕巴,怎么办请问有人听说过Sapphire Technologies吗FY2012 H-1B申请收件图表【紧急求助】收到面试电话但没听清是哪个职位,这该肿么办?现在还没有 clrs 3rd ed 的影印版卖阿能不能两人在一个公司工作面试感觉不错,结果仍然被拒求助:怎么拖offer问一道题目(3)公司改名,要H1 transfer吗SAS Job Opportunity有什么技巧能顺利通过behavioral assessment么?重新读一个学位OPT可以重新申请吗上次的博彩咋地咧?请问能否挂靠一个语言学校用CPT马上工作