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];
相关阅读
Twitter一个employee就能deactivate trump的Twitter?每天一发 我们的目标是用刷题绑架所有公司的面试正告版友 蔡宝健依旧在 多少度夕阳红LinkedIn 是默剧吗跳槽 H1B transfer 可以要求新公司 PP(premium process ) 吗?总结了我一把年纪混得这么惨的原因h1b transfer 诡异情况 求助leetcode题目视频讲解 100题纪念帖!哭哭 悲剧了每天一发 我们的目标是用刷题绑架所有公司的面试果家面试需要3个推荐人是什么情况?请问微软sdeII都会面些啥?Lc 699做了一个小时才通过员工薪水不满意排行榜, 软软和果果排名靠前在职跳槽怎么这么难?找不到正经公司,去icc怎么样?Leetcode 689居然是fb的高频题?人是环境的产物 (转载)求助:data scientist的H1B transfer被RFE了!怎么办???NY/NJ Java Consultant C2C $90/hr