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 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 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 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 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 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 不知道标准的shuffle card是什么算法
: 我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
: 那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
: 面一半和后面一般间隔混起来?
: void fun()
: {
: .....
: for ( int i =1; i
: card_after[i]=card_before[i];
相关阅读
千老可以过的比绝大部分码工舒服 请转找工版 (转载)求问一个公司靠不靠谱——BA Technolinks Corp郁闷中说说最近fail的面试吧(update) 在办opt,驾照renew是拿着I-20 opt就可以,还是需要等opt下来请问ICC会负责培训菜鸟吗?包子答谢跟老板住一个小区是不是不太好?要是正好人在加州,可以和recruiter直接商量上门面试吗?有已经在ITU上课的同学吗?offer file h1b transfer后反悔可以吗或者4个月申请2ci h1b transfer可以吗怎么看一个start up现在的市值?比方说这家面试问题求教有startup 招SE么?对一些烂大街了的面试题, 要注意伪装实习的offer的start date可以临时往后推吗?请问这句话怎么翻译?版上有做ruby on rails的么?累不累诚心请教OPT转CPT的问题,感激不尽弱弱的问一下,那种职位上写着contract的公司,是不是干完几个月就走人了?h1b 13132 还在initial reviewG终于悲剧