Permutation and combination is the most basic concept of combinatorial learning. The so-called arrangement refers to the ordering of the specified number of elements from the given number of elements. The combination refers to the only number of elements from the given number of elements, regardless of the order. The central problem with permutation combinations is to study the total number of possible scenarios for the permutations and combinations of a given requirement. The permutation combination is closely related to the classical probability theory.
This paper uses PHP to solve the mathematical problem is to figure out C (a,1) * C (b, 1) * ... * C (n, 1) combination, where C (n, 1) represents any element from n elements
A few days ago because the business needs to write a calculation of the combination of permutations of code, today, to prepare a bit for later use
The code is as follows:
<?php/** * Math problem to solve: Calculate C (a,1) * C (b, 1) * ... * combination of C (n, 1), where C (n, 1) represents an arbitrary element from n elements * * Examples of practical problems to solve: A grade has m classes, the number of each class Different, now to draw from each class A person to form a group, * by the group to represent the grade to participate in the school's activities, please give all possible combinations *//* ################################### start Calculate ################################### *//** * Array Description: The array is a two-dimensional array, the first-dimensional index represents the class number, 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", "Stud Ent43 ") */* calculate C (a,1) * C (b, 1) * ... * C (n, 1) value */$CombineCount = 1;foreach ($CombinList as $Key + $Value) {$Combin Ecount *= count ($Value);} $RepeatTime = $CombineCount, foreach ($CombinList as $ClassNo + $StudentList) {//$StudentList the maximum number of repetitions that occur vertically after splitting a component into a combination $RepeatTime = $RepeatTime/count ($StudentList); $StartPosition = 1; Start looping for 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 result */echo "<pre>";p Rint_r ($Result);? >