[cc lang="php"]
private function combination($ar, $num) {
$control = range(0, $num-1);
$k = false;
$total = count($ar);
while($control[0] < $total-($num-1)) {
$t = array();
for($i=0; $i <$num; $i++) $t[] = $ar[$control[$i]];
$r[] = $t;
for($i=$num-1; $i>=0; $i--) {
$control[$i]++;
for($j=$i; $j <$num-1; $j++) $control[$j+1] = $control[$j]+1;
if($control[$i] < $total-($num-$i-1)) break;
}
}
return $r;
} private function combination($ar, $num) {
$control = range(0, $num-1);
$k = false;
$total = count($ar);
while($control[0] < $total-($num-1)) {
$t = array();
for($i=0; $i <$num; $i++) $t[] = $ar[$control[$i]];
$r[] = $t;
for($i=$num-1; $i>=0; $i--) {
$control[$i]++;
for($j=$i; $j <$num-1; $j++) $control[$j+1] = $control[$j]+1;
if($control[$i] < $total-($num-$i-1)) break;
}
}
return $r;
}[/cc]
No comments:
Post a Comment