Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Combinations/PHP/combinations-1.php
Normal file
49
Task/Combinations/PHP/combinations-1.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
$a=array(1,2,3,4,5);
|
||||
$k=3;
|
||||
$n=5;
|
||||
$c=array_splice($a, $k);
|
||||
$b=array_splice($a, 0, $k);
|
||||
$j=$k-1;
|
||||
print_r($b);
|
||||
|
||||
while (1) {
|
||||
|
||||
$m=array_search($b[$j]+1,$c);
|
||||
if ($m!==false) {
|
||||
$c[$m]-=1;
|
||||
$b[$j]=$b[$j]+1;
|
||||
print_r($b);
|
||||
}
|
||||
if ($b[$k-1]==$n) {
|
||||
$i=$k-1;
|
||||
while ($i >= 0) {
|
||||
|
||||
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
|
||||
|
||||
$m=array_search($b[$i]+1,$c);
|
||||
if ($m!==false) {
|
||||
$c[$m]=$c[$m]-1;
|
||||
$b[$i]=$b[$i]+1;
|
||||
|
||||
$g=$i;
|
||||
while ($g != $k-1) {
|
||||
array_unshift ($c, $b[$g+1]);
|
||||
$b[$g+1]=$b[$g]+1;
|
||||
$g++;
|
||||
}
|
||||
$c=array_diff($c,$b);
|
||||
print_r($b);
|
||||
break;
|
||||
}
|
||||
$i--;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
48
Task/Combinations/PHP/combinations-2.php
Normal file
48
Task/Combinations/PHP/combinations-2.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
function combinations_set($set = [], $size = 0) {
|
||||
if ($size == 0) {
|
||||
return [[]];
|
||||
}
|
||||
|
||||
if ($set == []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
$prefix = [array_shift($set)];
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach (combinations_set($set, $size-1) as $suffix) {
|
||||
$result[] = array_merge($prefix, $suffix);
|
||||
}
|
||||
|
||||
foreach (combinations_set($set, $size) as $next) {
|
||||
$result[] = $next;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function combination_integer($n, $m) {
|
||||
return combinations_set(range(0, $n-1), $m);
|
||||
}
|
||||
|
||||
assert(combination_integer(5, 3) == [
|
||||
[0, 1, 2],
|
||||
[0, 1, 3],
|
||||
[0, 1, 4],
|
||||
[0, 2, 3],
|
||||
[0, 2, 4],
|
||||
[0, 3, 4],
|
||||
[1, 2, 3],
|
||||
[1, 2, 4],
|
||||
[1, 3, 4],
|
||||
[2, 3, 4]
|
||||
]);
|
||||
|
||||
echo "3 comb 5:\n";
|
||||
foreach (combination_integer(5, 3) as $combination) {
|
||||
echo implode(", ", $combination), "\n";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue