PHP--有4个数字:1、2、3、4,能组成多少个互不相同且不重复数字的三位数?各是多少

时间:2021-06-25 16:41:48   收藏:0   阅读:0
 1 <?php
 2 $str = ‘1234‘;
 3 $ar =str_split($str);
 4 perm($ar, 0, count($ar)-1);
 5 
 6 function perm(&$ar, $now, $length) {
 7     if($now == $length){
 8         $str=substr(join("",$ar),0,3);
 9         echo $str, PHP_EOL;
10     }else {
11         for($i=$now; $i<=$length; $i++) {
12             swap($ar[$now], $ar[$i]);
13             perm($ar, $now+1, $length);
14             swap($ar[$now], $ar[$i]);
15         }
16     }
17 }
18 function swap(&$a, &$b) {
19     $c = $a;
20     $a = $b;
21     $b = $c;
22 }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!