Php recursion to get all possibilities of strings

后端 未结 6 1851
失恋的感觉
失恋的感觉 2020-11-27 07:22

Here is my code to get all possibilities:

$seq[1] = \'d\';
$seq[2] = \'f\';
$seq[3] = \'w\';
$seq[4] = \'s\';

for($i = 1; $i < 5; $i++)
{
    $s[\'length         


        
6条回答
  •  借酒劲吻你
    2020-11-27 08:14

    Here it is:

    =$iaz+1; $jin--)
                        {
                            $a[$jin] = $a[$jin-1];
                        }
                    $a[$iaz] = $temp;
                    $ken = $ken%$f;
                }
            $t=1;
    
               // Let’s start creating a word combination: we have all the  necessary positions
            $newtext="";
    
            // Here is the while loop that creates the word combination
            while ($t<=$x)
                {
                    $newtext.=$characters[$a[$t]-1]."$space";
                    $t++;
                }
            $combinations[] =  $newtext ;
        }
    
            return $combinations;
    
    }
    
    function fact($a){
    if ($a==0) return 1;
    else return $fact = $a * fact($a-1);
    }
    
    $a = combinations("d,f,w,s","");
        foreach ($a as $v) {
                echo "$v"."\n";
        }
    
    ?>
    

    Output:

    dfws
    dfsw
    dwfs
    dwsf
    dsfw
    dswf
    fdws
    fdsw
    fwds
    fwsd
    fsdw
    fswd
    wdfs
    wdsf
    wfds
    wfsd
    wsdf
    wsfd
    sdfw
    sdwf
    sfdw
    sfwd
    swdf
    swfd
    

    Also, read this;

提交回复
热议问题