您好,欢迎访问一九零五行业门户网

使用php计算排列组合的方法_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         {
            for($i = 0; $i             {
               $result[$tempstartposition + $i][$classno] = $student;
            }
            $tempstartposition += $repeattime * count($studentlist);
        }
        $startposition += $repeattime;
    }
}
/* 打印结果 */
echo ;
print_r($result);
?>

http://www.bkjia.com/phpjc/825191.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/825191.htmltecharticle前些天因为业务需要写了一段计算排列组合的代码,今天整理了一下,以备后用 复制代码 代码如下: ?php /** * 要解决的数学问题 :算出c(a...
其它类似信息

推荐信息