Example:
Players computer
2 5
1
43 2
4 1
5 3
CodePudding user response:
Shuffle algorithm, refer to the following:# include & lt; Stdio. H>
#include
#include
Int d [6];
Int I, n, a, b, t;
C, int 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 for n! */
J *=n * 2;
For (c=1; C<=j; C + +) {/* test n * 2 * n! Time */
for (i=0; iFor (I=n; i> 0; I -) {/* disturb 0 ~ */n - 1
A=I - 1; B=rand () % I;
If (a! A=b) {t=d [a]; D [a] [b]=d; D [b]=t; }
}
Printf (" % 4 d: ", c);
for (i=0; iprintf("\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 for n! */
J *=n * 2;
For (c=1; C<=j; C + +) {/* test n * 2 * n! Time */
For (I=1; i<=n; I++) d [I]=I;/* fill in 1 ~ n */
For (I=n; i> 1; I -) {/* disturb 1 ~ n */
A=I; B=rand () % I + 1;
If (a! A=b) {t=d [a]; D [a] [b]=d; D [b]=t; }
}
Printf (" % 4 d: ", c);
For (I=1; i<=n; I++) printf (" % d ", d [I]);
printf("\n");
}
}
}