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
/**
* 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