排列-所有可能的数字集我有数字,从0到8。结果,所有可能的集合,每组都应该使用所有的数字,每个数字只能在一组中出现一次。我希望看到用PHP编写的解决方案可以打印出结果。或者,至少,我希望在组合学理论上有所补充,因为我早已忘记了这一点。计算有多少排列的公式是什么?示例集:0-1-2-3-4-5-6-7-80-1-2-3-4-5-6-8-70-1-2-3-4-5-8-6-70-1-2-3-4-8-5-6-70-1-2-3-8-4-5-6-70-1-2-8-3-4-5-6-7以此类推.。
3 回答
斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
nPk = n!/(n-k)!
pc_permute(array(0, 1, 2, 3, 4, 5, 7, 8));
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
print join(' ', $perms) . "\n";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}}
慕斯王
TA贡献1864条经验 获得超2个赞
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
$return = array($perms);
} else {
$return = array();
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$return = array_merge($return, pc_permute($newitems, $newperms));
}
}
return $return;}$value = array('1', '2', '3');print_r(pc_permute($value));- 3 回答
- 0 关注
- 430 浏览
添加回答
举报
0/150
提交
取消
