Chinaunix

标题: 计算排列组合的php代码 [打印本页]

作者: liyupeng    时间: 2005-07-15 17:46
标题: 计算排列组合的php代码
前些天因为业务需要写了一段计算排列组合的代码,今天整理了一下,以备后用
array("Student10", "Student11"),
                    2 => array("Student20", "Student21", "Student22"),
                    3 => array("Student30"),
                    4 => array("Student40", "Student41", "Student42", "Student43"));
/* 计算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中的元素在拆分成组合后纵向出现的最大重复次数
    $RepeatTime = $RepeatTime / count($StudentList);
    $StartPosition = 1;
    // 开始对每个班级的学生进行循环
    foreach($StudentList as $Student)
    {
        $TempStartPosition = $StartPosition;
        $SpaceCount = $CombineCount / count($StudentList) / $RepeatTime;
        for($J = 1; $J ";
print_r($Result);
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1777/showart_35855.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2