douyan4470 2013-02-21 08:18
浏览 42
已采纳

php中阶乘的排列

Don't know how to explain. But maybe example below will be make you understandable what my problem is.

Example :

I have an array with 3 elements.

$elements = array( 'A', 'B', 'C' );

The permutation will be 3 in 3. so the result are :

A-B-C ; A-C-B ; B-A-C ; B-C-A ; C-A-B; C-B-A

I don't want any permutation 2 in 3 or 1 in 3, just 3 in 3 as you can see in example. So if I have 4 elements in an array, the permutation is 4 in 4. and so on...

(I think the number of permutations is 3! = 1*2*3 = 6 permutations, 4! = 1*2*3*4 = 24 permutations... that why I call Permutations of Factorial.)

If there are other question and answer similar to my problem, please let me know

  • 写回答

2条回答 默认 最新

  • dsgw8802 2013-02-21 08:59
    关注

    Use a recursive function:

    function permutations($elements) {
        if(count($elements)<2) return $elements;
    
        $newperms= array();
        foreach($elements as $key=>$element) {
            $newelements= $elements;
            unset($newelements[$key]);
    
            $perms= permutations($newelements);
            foreach($perms as $perm) {
                $newperms[]= $element."-".$perm;
            }
        }
        return $newperms;
    }
    

    Didn't test it, so there is still work for you ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题