Use php to calculate the arrangement and combination. The previous day, because the business needs to write a piece of code for calculating the arrangement and combination, I sorted it out today, and then copied the code as follows :? Php *** mathematical problem to be solved: Calculate C (a wrote a piece of code for computing arrangement and combination that was required by the business a few days ago. I sorted it out today for future use.
The code is as follows:
/**
* Mathematical problem to be solved: Calculate C (a, 1) * C (B, 1 )*... * The combination of C (n, 1), where C (n, 1) represents any element from n
*
* Example of the actual problem to be solved: there are m classes in a certain grade, and the number of students in each class is different. now we need to select a person from each class to form a group,
* This group is used to represent a certain school activity in this grade. please provide all possible combinations.
*/
/* ################################### Start Computing # ##################################*/
/**
* Array to be sorted 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 "),
2 => array ("Student20", "Student21", "Student22 "),
3 => array ("Student30 "),
4 => array ("Student40", "Student41", "Student42", "Student43 "));
/* 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)
{
// $ StudentList maximum number of times the elements appear vertically after being split into combinations
$ RepeatTime = $ RepeatTime/count ($ StudentList );
$ StartPosition = 1;
// Start to cycle the students in each class
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;
}
}
/* Print the result */
Echo"
";
Print_r ($ Result );
?>
The pipeline code is as follows :? Php/*** mathematical problem to be solved: Calculate C (...