m*******a 发帖数: 83 | 1 从10个不同的球中任意取5个球。算法如何实现呢? |
q*****g 发帖数: 72 | 2 permutation. take first 5
【在 m*******a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 从10个不同的球中任意取5个球。算法如何实现呢?
|
m*******a 发帖数: 83 | 3 具体算法如何实现呢? 任何语言都可以
【在 q*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : permutation. take first 5
|
q*****g 发帖数: 72 | 4 for (int i = 1; i <= 10; +=i)
{
int j = rand() % (10-i+1) + i; // random number between i to 10
swap(a[i],a[j]);
}
// take first 5
【在 m*******a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 具体算法如何实现呢? 任何语言都可以
|
k****f 发帖数: 3794 | 5 你找找knuth的电子书,the art of computer programming volume 4
里面有一章就有详细介绍怎么产生排列组合的
【在 m*******a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 具体算法如何实现呢? 任何语言都可以
|
m*******a 发帖数: 83 | 6 能具体解释一下swap吗?
【在 q*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : for (int i = 1; i <= 10; +=i) : { : int j = rand() % (10-i+1) + i; // random number between i to 10 : swap(a[i],a[j]); : } : // take first 5
|
q*****g 发帖数: 72 | 7 void swap(int i, int j)
{
int temp;
temp = i;
i = j;
j = temp;
}
【在 m*******a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 能具体解释一下swap吗?
|
m*******a 发帖数: 83 | 8 I see, but if I take the first 5 of the randon arrangement, how to avoid the
repeated ones?
【在 q*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : void swap(int i, int j) : { : int temp; : temp = i; : i = j; : j = temp; : }
|
k****f 发帖数: 3794 | 9 google就有了
http://www.merriampark.com/comb.htm
the
【在 m*******a 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I see, but if I take the first 5 of the randon arrangement, how to avoid the : repeated ones?
|
O******e 发帖数: 734 | 10
The above line is strictly speaking problematic unless you can guarantee
that the low-order bits returned by rand() are uniformly randomized.
See either Chapter 3 of Knuth or Chapter 7 of Numerical Recipes or
"man -S 3 rand" on a Linux system for the correct way of randomly
selecting integers with uniform probability.
【在 q*****g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : for (int i = 1; i <= 10; +=i) : { : int j = rand() % (10-i+1) + i; // random number between i to 10 : swap(a[i],a[j]); : } : // take first 5
|
a*********e 发帖数: 697 | 11 agree, set random seed first.
【在 O******e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : The above line is strictly speaking problematic unless you can guarantee : that the low-order bits returned by rand() are uniformly randomized. : See either Chapter 3 of Knuth or Chapter 7 of Numerical Recipes or : "man -S 3 rand" on a Linux system for the correct way of randomly : selecting integers with uniform probability.
|