使用php計算排列組合的方法
更新時間:2013年11月13日 09:48:33 作者:
本文用PHP要解決的數(shù)學(xué)問題是算出C(a,1) * C(b, 1) * ... * C(n, 1)的組合情況,其中C(n, 1)代表從n個元素里任意取一個元素
前些天因為業(yè)務(wù)需要寫了一段計算排列組合的代碼,今天整理了一下,以備后用
<?php
/**
* 要解決的數(shù)學(xué)問題 :算出C(a,1) * C(b, 1) * ... * C(n, 1)的組合情況,其中C(n, 1)代表從n個元素里任意取一個元素
*
* 要解決的實際問題樣例:某年級有m個班級,每個班的人數(shù)不同,現(xiàn)在要從每個班里抽選一個人組成一個小組,
* 由該小組來代表該年級參加學(xué)校的某次活動,請給出所有可能的組合
*/
/* ################################### 開始計算 ################################### */
/**
* 需要進行排列組合的數(shù)組
*
* 數(shù)組說明:該數(shù)組是一個二維數(shù)組,第一維索引代表班級編號,第二維索引代表學(xué)生編號
*/
$CombinList = array(1 => 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中的元素在拆分成組合后縱向出現(xiàn)的最大重復(fù)次數(shù)
$RepeatTime = $RepeatTime / count($StudentList);
$StartPosition = 1;
// 開始對每個班級的學(xué)生進行循環(huán)
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;
}
}
/* 打印結(jié)果 */
echo "<pre>";
print_r($Result);
?>
復(fù)制代碼 代碼如下:
<?php
/**
* 要解決的數(shù)學(xué)問題 :算出C(a,1) * C(b, 1) * ... * C(n, 1)的組合情況,其中C(n, 1)代表從n個元素里任意取一個元素
*
* 要解決的實際問題樣例:某年級有m個班級,每個班的人數(shù)不同,現(xiàn)在要從每個班里抽選一個人組成一個小組,
* 由該小組來代表該年級參加學(xué)校的某次活動,請給出所有可能的組合
*/
/* ################################### 開始計算 ################################### */
/**
* 需要進行排列組合的數(shù)組
*
* 數(shù)組說明:該數(shù)組是一個二維數(shù)組,第一維索引代表班級編號,第二維索引代表學(xué)生編號
*/
$CombinList = array(1 => 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中的元素在拆分成組合后縱向出現(xiàn)的最大重復(fù)次數(shù)
$RepeatTime = $RepeatTime / count($StudentList);
$StartPosition = 1;
// 開始對每個班級的學(xué)生進行循環(huán)
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;
}
}
/* 打印結(jié)果 */
echo "<pre>";
print_r($Result);
?>
相關(guān)文章
PHP array_reduce()函數(shù)的應(yīng)用解析
這篇文章主要介紹了PHP array_reduce()的應(yīng)用,本文通過代碼舉例給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10
ThinkPHP通過AJAX返回JSON的兩種實現(xiàn)方法
這篇文章主要介紹了ThinkPHP通過AJAX返回JSON的兩種實現(xiàn)方法,分析了ThinkPHP中內(nèi)置的ajaxReturn函數(shù)的用法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
php結(jié)合GD庫簡單實現(xiàn)驗證碼的示例代碼
這篇文章主要介紹了php結(jié)合GD庫簡單實現(xiàn)驗證碼的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

