CodePudding user response:
Probably like this
#include//introduce bool type
Bool chachong (arr int * and an int index, size_t n)
{
if (! The index | |! N)//ignore the zero value and the first
{
return true;
}
While (n)//find duplicate values from an array
{
If (arr==[n - 1] index)
{
return true;
}
N -;
}
return false;
}
////////////////
Int arr [13]={0};
Size_t I=0;
While (I & lt; 13)
{
Do
{
Arr [I]=rand () % of 55;
} while (chachong (arr, arr [I], I));
i++;
}
CodePudding user response:
Must be repeated random, the so-called "no" is actually a shuffle, repeat random shuffle algorithm is used to 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");
}
}
}
CodePudding user response:
After random order directly to the front