public function main() { $list=[ [7,1,3],//第一位 [5,],//第二位 [1],//第三位 [3,5],//第四位 [4,6]//第五位 ]; $res=$this->pailie($list); dump($res); } public function pailie ($CombinList){ /* 计算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 <= $SpaceCount; $J++) { for ($I = 0; $I < $RepeatTime; $I++) { $Result[$TempStartPosition + $I][$ClassNo] = $Student; } $TempStartPosition += $RepeatTime * count($StudentList); } $StartPosition += $RepeatTime; } } /* 返回结果 */ $result=[]; foreach($Result as $value){ $result[]=implode(',',$value); } return $result; }
将二维数组排列组合
最新推荐文章于 2021-03-15 22:44:59 发布