如何在一组数字中的6个之间不重复地获得所有有序组合?

时间:2018-06-15 15:03:33

标签: php

有一组有序数字,比如$nums = array(3, 7, 10, 12, 20, 24, 26, 34, 37);

我希望从上面的数组获得 6 数字的所有可能组合,而顺序是从最低数字到最大数字;例如:

3, 7, 10, 12, 20, 24
3, 7, 10, 12, 20, 26
3, 7, 10, 12, 20, 34
3, 7, 10, 12, 20, 37
3, 7, 10, 12, 24, 26
3, 7, 10, 12, 24, 34
3, 7, 10, 12, 24, 37
3, 7, 10, 12, 26, 34
3, 7, 10, 12, 26, 37
3, 7, 10, 12, 34, 37
3, 7, 10, 20, 24, 26
3, 7, 10, 20, 24, 34
3, 7, 10, 20, 24, 37
3, 7, 10, 20, 26, 34
3, 7, 10, 20, 26, 37
3, 7, 10, 20, 34, 37
etc...

1 个答案:

答案 0 :(得分:0)

听起来你需要计算出数组长度的 factorial ,这样就可以解决这个问题:

function factorial($num){
    $factorial = 1;
    for ($x=$num; $x>=1; $x--) 
    {
        $factorial = $factorial * $x;
    }
}

$result = factorial(count($nums));