<pre id=\"best-answer-content\" class=\"reply-text mb10\">#include stdio.h
#include stdlib.h
#include time.h
#define N 12
int main(int argc, char *argv[])
{
int i;
int x[N] = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
printf(\"之前数组: \");
for (i = 0; iN; i)
printf(\"=\", x);
printf(\"\\n\");
srand((unsigned)time(NULL));
for (i = N-1; i = 0; i--)
{
int rd = rand() % N;
int temp = x;
x = x [rd];
x[rd] = temp;
}
printf(\"之后数组: \");
for (i = 0; iN; i)
printf(\"=\", x);
printf(\"\\n\");
return 0;
}
——————————————————————————————————
其实该问题类似洗牌的程序,所有的数字其实都是已知的,只需要通过随机数去打乱这些数的次序就可以达到效果。先前的regret4hxp2的思路可以达到同样的效果,但是有个缺点是效率不高,而且需要引入同步的计数。 |