- 论坛徽章:
- 0
|
function permutation($allArray, $selectedCount) {
global $result,$resultWhole;
$length = count($allArray);
if( $selectedCount<1
|| $length<$selectedCount ) return;
for($i=0;$i<$length;$i++) {
$result[$selectedCount-1] = $allArray[$i];
if( $selectedCount==1 ) {
$newResult = $result;
sort($newResult);
reset($newResult);
echo "ffff";
if(!in_array($newResult,$resultWhole)) {
$resultWhole[]=$newResult;
for($j=0;$j<count($newResult);$j++) {
echo " ".$newResult[$j];
}
echo " end<br>;";
}
continue;
} else {
$newArray = $allArray;
array_splice($newArray, $i, 1);
permutation($newArray, $selectedCount-1);
}
}
}
$result = array();
$resultWhole = array();
$all = array(12,23,34,45,56,67,21,32,43,54,65,76); //元素集合
$selectedCount = 6; //所要选择的元素个数
permutation($all,$selectedCount); //组合
如果$all填10个以内的数字都没有多大问题,如果是12个数字,就相当慢,而且还会报Maximum execution time of 30 seconds exceeded in C:\www\pc.php on line 17
就是把时间设的很大也不行,它就会死在哪里,17行就是in_array($newResult,$resultWhole,请大家帮忙看看,谢谢 |
|