An arrangement and combination algorithm has been learned in middle school. & nbsp; arrangement and combination & nbsp; 1 & nbsp; 2 & nbsp; 3 & nbsp; 4 & nbsp; 5 & nbsp; 6. you can construct 720 different strings. here we write a rough implementation method & nbsp; for example, & nbsp; 1-& nbsp; 6. Which of the following experts can you advise; question about a permutation and combination algorithm
In middle school, I learned how to arrange and combine 1, 2, 3, 4, 6, and can form 720 different strings.
Here we write a rough implementation method example: 1-6
Hope you can give me some advice.
$ J = 0;
For ($ I = 123456; $ I <= $ number; $ I ++ ){
$ Box = array ();
$ Ge = $ I % 10;
If ($ ge> 6 | $ ge = 0 ){
Continue;
}
$ Box [] = $ ge;
$ Shi = intval ($ I/10) % 10;
If ($ shi> 6 | $ shi = 0 ){
Continue;
}
$ Box [] = $ shi;
$ Bai = intval ($ I/100) % 10;
If ($ bai> 6 | $ bai = 0 ){
Continue;
}
$ Box [] = $ bai;
$ Qian = intval ($ I/1000) % 10;
If ($ qian> 6 | $ qian = 0 ){
Continue;
}
$ Box [] = $ qian;
$ Wan = intval ($ I/10000) % 10;
If ($ wan> 6 | $ wan = 0 ){
Continue;
}
$ Box [] = $ wan;
$ Shiwan = intval ($ I/100000 );
If ($ shiwan> 6 | $ shiwan = 0 ){
Continue;
}
$ Box [] = $ shiwan;
// Echo count (array_unique ($ box ));
// Echo'
';
// Var_dump (array_unique ($ box ));
// Echo'
';
If (count (array_unique ($ box ))! = 6 ){
Continue;
}
If ($ j % 10 = 0 ){
Echo" ";
}
$ J ++;
Echo '', $ I ,'';
}
Echo" ";
Echo "{$ j} in total ";
------ Solution --------------------
$a = perm(array(1,2,3,4,5,6));
echo count($a);
//print_r($a);
function perm($list, $k=0, $m=0) {
if(! $m) $m = count($list) - 1;
$r = array();
if($k >= $m) {
$r[] = join('', $list);
}else {
for($i = $k; $i <= $m; $i++) {
list($list[$k], $list[$i]) = array($list[$i], $list[$k]);
$r = array_merge($r, perm($list, $k + 1, $m));
list($list[$k], $list[$i]) = array($list[$i], $list[$k]);
}
}
return $r;
}
720