Home >Backend Development >PHP Tutorial >How to calculate permutations and combinations using PHP_PHP Tutorial

How to calculate permutations and combinations using PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:14974browse

A few days ago, I wrote a piece of code to calculate permutations and combinations due to business needs. I sorted it out today for future use

Copy the code The code is as follows:

/**
* Mathematical problem to be solved: Calculate the combination of C(a,1) * C(b, 1) * ... * C(n, 1), where C(n, 1) represents n Select any element from the element
*
* Example of the practical problem to be solved: There are m classes in a certain grade, and the number of people in each class is different. Now we need to select one person from each class to form a group. ,
* The group will represent the grade to participate in a school activity, please give all possible combinations
*/
/* ####################### ########### Start calculating################################## */
/**
* Arrays that need to be arranged and combined
*
* Array description: This array is a two-dimensional array. The first-dimensional index represents the class number, and the second-dimensional index represents the student number
*/
$CombinList = array(1 => array("Student10", "Student11"),
                                                                                                                                                         $CombinList        , "Student22"),
                                                                                                                                                                                                                                                                            /* Calculate the value of 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 )
{
// The maximum number of vertical repetitions of elements in $StudentList after splitting into combinations
$RepeatTime = $RepeatTime / count($StudentList);
$StartPosition = 1;
// Start looping through students in each class
foreach($StudentList as $Student)
{
$TempStartPosition = $StartPosition;
$SpaceCount = $CombineCount / count($ StudentList) / $RepeatTime;
for($J = 1; $J <= $SpaceCount; $J ++)
$I ++)
                                                                                                                                                                                                                                                                                                count($StudentList);
}
}
$StartPosition += $RepeatTime;
}
}
/* Print result*/
echo "
";
print_r($Result);
?>





http://www.bkjia.com/PHPjc/825191.html
www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/825191.htmlTechArticleA few days ago, I wrote a piece of code to calculate permutations and combinations due to business needs. I sorted it out today for future use. Copy the code as follows: ?php /** * Math problem to be solved: Calculate C(a...
Statement:
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