用php计算排列组合的方法

前几天因为业务需要写了一段计算组合的代码,今天整理一下供以后使用。
复制代码代码如下所示:
< PHP

*解决数学问题:计算C(A,1)* C(B,1)*的组合…* C(n,1),其中C(n,1)表示n元素的一个元素。
*
*例子来解决实际问题:有M级的班级,每个班级的人数都不一样,现在每个班都选一个人作为一个团队,
*本组代表本年级的一项学校活动,请提供所有可能的组合。
* /
###################################计算################################### / * * /

*需要排列的数组。
*
*数组描述:这个数组是一个二维数组,第一个维索引代表类号,第二个索引代表学生编号。
* /
combinlist美元=阵列(1 =阵列(student10
2 =阵列(student20
3 =阵列(student30 ),
4 =阵列(student40
*(a,1)C *(b,1)*…* C(n,1)的值。
combinecount = 1美元;
foreach(combinlist美元美元美元关键=价值)
{
combinecount美元(美元值)=计数;
}
repeattime = combinecount美元美元;
foreach(combinlist美元美元美元分类号= studentlist)
{
重复/ $ studentlist元素出现在纵向分为组合的最大数量
repeattime美元=美元repeattime /计数($ studentlist);
启动位置= 1美元;
每一班学生的开始/循环
foreach(studentlist美元美元的学生)
{
tempstartposition美元= $启动位置;
spacecount美元=美元combinecount /计数($ studentlist)/ repeattime美元;
(J = 1;对美元美元美元,<< spacecount;j++)
{
为($我= 0;美元美元美元我<< repeattime;i++)
{
结果tempstartposition美元美元美元{我} { $ +分类号} = $学生;
}
tempstartposition美元=美元repeattime *计数($ studentlist);
}
repeattime美元美元的启动位置=;
}
}
打印

print_r($结果);
>