Calculation of arrangement and combination using php _ php instance

Source: Internet
Author: User
The mathematical problem to be solved with PHP in this article is to calculate C (a, 1) * C (B, 1 )*... * combination of C (n, 1), where C (n, 1) it means to retrieve any element from n elements. the previous day, because the business needs to write a piece of code for computing arrangement and combination, 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);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.