时时商务社区
标题:
使用php计算排列组合的方法
[打印本页]
作者:
qz234
时间:
2018-2-14 06:01
前些天因为业务需要写了一段计算排列组合的代码,今天整理了一下,以备后用
[U]复制代码[/U] 代码如下:
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);
?>
您可能感兴趣的文章:
组合算法的PHP解答方法
php全排列递归算法代码
php生成数组的使用示例 php全组合算法
php通过排列组合实现1到9数字相加都等于20的方法
php求数组全排列,元素所有组合的方法
PHP输出多个元素的排列或组合的方法
php求数组全排列,元素所有组合的方法总结
浅谈PHP的排列组合(如输入a,b,c 输出他们的全部组合)
PHP实现的简单排列组合算法应用示例
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2