Home > Backend Development > PHP Tutorial > Detailed explanation of PHP permutation recursion and permutation and combination example codes

Detailed explanation of PHP permutation recursion and permutation and combination example codes

伊谢尔伦
Release: 2023-03-14 08:58:02
Original
2154 people have browsed it

1. Arrangement recursion

If P represents the full arrangement of n elements, and Pi represents the full arrangement of n elements that does not include element i, (i) Pi means adding in front of the arrangement Pi Arrangement with the prefix i, then the total arrangement of n elements can be recursively defined as:
① If n=1, then the arrangement P has only one element i;
② If n>1, then the total arrangement P is represented by the arrangement ( i) Pi composition;
According to the definition, it can be seen that if the arrangement Pi of (k-1) elements has been generated, then the arrangement of k elements can be generated by adding element i in front of each Pi.

Code:

function rank($base, $temp=null)
{
    $len = strlen($base);
    if($len <= 1)
    {
        echo $temp.$base.&#39;<br/>&#39;;
    }
    else
    {
        for($i=0; $i< $len; ++$i)
        {
            rank(substr($base, 0, $i).substr($base, $i+1, $len-$i-1), $temp.$base[$i]);
        }
    }
}
rank(&#39;123&#39;);
Copy after login

However, after multiple test runs, it was found that there is a problem: if there are the same elements, the entire arrangement will be repeated.
For example, there are only three situations for the full arrangement of '122': '122', '212', and '221'; but the above method is repeated.
Slightly modified, added a flag to determine duplication, solved the problem (the code is as follows):

function fsRank($base, $temp=null)
{
    static $ret = array();
    $len = strlen($base);
    if($len <= 1)
    {
        //echo $temp.$base.&#39;<br/>&#39;;
        $ret[] = $temp.$base;
    }
    else
    {
        for($i=0; $i< $len; ++$i)
        {
            $had_flag = false;
            for($j=0; $j<$i; ++$j)
            {
                if($base[$i] == $base[$j])
                {
                    $had_flag = true;
                    break;
                }
            }
            if($had_flag)
            {
                continue;
            }
            fsRank(substr($base, 0, $i).substr($base, $i+1, $len-$i-1), $temp.$base[$i]);
        }
    }
    return $ret;
}
print &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r(fsRank(&#39;122&#39;));
print &#39;
';
Copy after login

2. Permutation and combination examples

<?php
/**
 * 要解决的数学问题    :算出C(a,1) * C(b, 1) * ... * C(n, 1)的组合情况,其中C(n, 1)代表从n个元素里任意取一个元素
 *
 * 要解决的实际问题样例:某年级有m个班级,每个班的人数不同,现在要从每个班里抽选一个人组成一个小组,
 *                       由该小组来代表该年级参加学校的某次活动,请给出所有可能的组合
 */
/* ################################### 开始计算 ################################### */
/**
 * 需要进行排列组合的数组
 *
 * 数组说明:该数组是一个二维数组,第一维索引代表班级编号,第二维索引代表学生编号
 */
$CombinList = array(1 => array("Student10", "Student11"),
                    2 => array("Student20", "Student21", "Student22"),
                    3 => array("Student30"),
                    4 => array("Student40", "Student41", "Student42", "Student43"));
/* 计算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */
$CombineCount = 1;
foreach($CombinList as $Key => $Value)
{
    $CombineCount *= count($Value);
}
$RepeatTime = $CombineCount;
foreach($CombinList as $ClassNo => $StudentList)
{
    // $StudentList中的元素在拆分成组合后纵向出现的最大重复次数
    $RepeatTime = $RepeatTime / count($StudentList);
    $StartPosition = 1;
    // 开始对每个班级的学生进行循环
    foreach($StudentList as $Student)
    {
        $TempStartPosition = $StartPosition;
        $SpaceCount = $CombineCount / count($StudentList) / $RepeatTime;
        for($J = 1; $J <= $SpaceCount; $J ++)
        {
            for($I = 0; $I < $RepeatTime; $I ++)
            {
               $Result[$TempStartPosition + $I][$ClassNo] = $Student;
            }
            $TempStartPosition += $RepeatTime * count($StudentList);
        }
        $StartPosition += $RepeatTime;
    }
}
/* 打印结果 */
echo "<pre class="brush:php;toolbar:false">";
print_r($Result);
?>
Copy after login

The above is the detailed content of Detailed explanation of PHP permutation recursion and permutation and combination example codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template