#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
d[6];
int
i,n,a,b,t;
int
c,j;
void
main() {
srand
(
time
(NULL));
printf
(
"shuffle 0..n-1 demo\n"
);
for
(n=1;n<=5;n++) {
/* 测试1~5个元素 */
printf
(
"_____n=%d_____\n"
,n);
j=1;
for
(c=1;c<=n;c++) j=j*c;
/* j为n! */
j*=n*2;
for
(c=1;c<=j;c++) {
/* 测试n*2*n!次 */
for
(i=0;i<n;i++) d[i]=i;
/* 填写0~n-1 */
for
(i=n;i>0;i--) {
/* 打乱0~n-1 */
a=i-1;b=
rand
()%i;
if
(a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf
(
"%04d:"
,c);
for
(i=0;i<n;i++)
printf
(
"%d"
,d[i]);
printf
(
"\n"
);
}
}
printf
(
"shuffle 1..n demo\n"
);
for
(n=1;n<=5;n++) {
/* 测试1~5个元素 */
printf
(
"_____n=%d_____\n"
,n);
j=1;
for
(c=1;c<=n;c++) j=j*c;
/* j为n! */
j*=n*2;
for
(c=1;c<=j;c++) {
/* 测试n*2*n!次 */
for
(i=1;i<=n;i++) d[i]=i;
/* 填写1~n */
for
(i=n;i>1;i--) {
/* 打乱1~n */
a=i;b=
rand
()%i+1;
if
(a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf
(
"%04d:"
,c);
for
(i=1;i<=n;i++)
printf
(
"%d"
,d[i]);
printf
(
"\n"
);
}
}
}