dpxkkhu1812 2018-11-02 15:17
浏览 97
已采纳

使用PHP生成所有可能的组合

I have a sequence of numbers interspersed with underscore:

_10_1_18_4_9_14_ _

I would like to replace the underscore with letters without touching the numbers, and generate the complete list of combinations, but I don't know how to do it.

Right now I have this:

$alph = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
echo $alph[0]."10".$alph[0]."1".$alph[0]."18".$alph[0]."4".$alph[0]."9".$alph[0]."14".$alph[0] .$alph[0];

Now, I don't know how to explore all the possible combinations.

There are 208 827 064 576 possible combinations and I have to put them all in a .txt file. That's why I avoid using "for"

Any ideas?

edit: For the example we can block the number of combinations to 100 for example with a $i ++ in the loop.

I know it's going to take a lot of time and space

  • 写回答

2条回答 默认 最新

  • douliao8402 2018-11-02 15:51
    关注

    This is probably not very efficient but it will get the job done:

    <?php
    $alph = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    $a = array();
    
    foreach( $alph as $l1 )
    {
        $a[0] = $l1.'10';
        foreach( $alph as $l2 )
        {
            $a[1] = $l2.'1';
            foreach( $alph as $l3 )
            {
                $a[2] = $l3.'18';
                foreach( $alph as $l4 )
                {
                    $a[3] = $l4.'4';
                    foreach( $alph as $l5 )
                    {
                        $a[4] = $l5.'9';
                        foreach( $alph as $l6 )
                        {
                            $a[5] = $l6.'14';
                            foreach( $alph as $l7 )
                            {
                                $a[6] = $l7;
                                foreach( $alph as $l8 )
                                {
                                    $a[7] = ' '.$l8; // your example shows a space, not sure if that was intentional
                                    // $line = implode( '', $a )."
    ";
                                    // Write $line to a file
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    Sample output:

    A10A1A18A4A9A14A A
    A10A1A18A4A9A14A B
    A10A1A18A4A9A14A C
    A10A1A18A4A9A14A D
    A10A1A18A4A9A14A E
    A10A1A18A4A9A14A F
    A10A1A18A4A9A14A G
    A10A1A18A4A9A14A H
    A10A1A18A4A9A14A I
    A10A1A18A4A9A14A J
    A10A1A18A4A9A14A K
    A10A1A18A4A9A14A L
    A10A1A18A4A9A14A M
    A10A1A18A4A9A14A N
    A10A1A18A4A9A14A O
    A10A1A18A4A9A14A P
    A10A1A18A4A9A14A Q
    A10A1A18A4A9A14A R
    A10A1A18A4A9A14A S
    A10A1A18A4A9A14A T
    A10A1A18A4A9A14A U
    A10A1A18A4A9A14A V
    A10A1A18A4A9A14A W
    A10A1A18A4A9A14A X
    A10A1A18A4A9A14A Y
    A10A1A18A4A9A14A Z
    A10A1A18A4A9A14B A
    A10A1A18A4A9A14B B
    A10A1A18A4A9A14B C
    A10A1A18A4A9A14B D
    A10A1A18A4A9A14B E
    A10A1A18A4A9A14B F
    A10A1A18A4A9A14B G
    A10A1A18A4A9A14B H
    A10A1A18A4A9A14B I
    A10A1A18A4A9A14B J
    A10A1A18A4A9A14B K
    A10A1A18A4A9A14B L
    A10A1A18A4A9A14B M
    A10A1A18A4A9A14B N
    A10A1A18A4A9A14B O
    A10A1A18A4A9A14B P
    A10A1A18A4A9A14B Q
    A10A1A18A4A9A14B R
    A10A1A18A4A9A14B S
    A10A1A18A4A9A14B T
    A10A1A18A4A9A14B U
    A10A1A18A4A9A14B V
    A10A1A18A4A9A14B W
    A10A1A18A4A9A14B X
    A10A1A18A4A9A14B Y
    A10A1A18A4A9A14B Z
    A10A1A18A4A9A14C A
    A10A1A18A4A9A14C B
    A10A1A18A4A9A14C C
    A10A1A18A4A9A14C D
    A10A1A18A4A9A14C E
    A10A1A18A4A9A14C F
    A10A1A18A4A9A14C G
    A10A1A18A4A9A14C H
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档