使用php計算排列組合的方法
更新時間:2013年11月13日 09:48:33 作者:
本文用PHP要解決的數學問題是算出C(a,1) * C(b, 1) * ... * C(n, 1)的組合情況,其中C(n, 1)代表從n個元素里任意取一個元素
前些天因為業(yè)務需要寫了一段計算排列組合的代碼,今天整理了一下,以備后用
<?php
/**
* 要解決的數學問題 :算出C(a,1) * C(b, 1) * ... * C(n, 1)的組合情況,其中C(n, 1)代表從n個元素里任意取一個元素
*
* 要解決的實際問題樣例:某年級有m個班級,每個班的人數不同,現在要從每個班里抽選一個人組成一個小組,
* 由該小組來代表該年級參加學校的某次活動,請給出所有可能的組合
*/
/* ################################### 開始計算 ################################### */
/**
* 需要進行排列組合的數組
*
* 數組說明:該數組是一個二維數組,第一維索引代表班級編號,第二維索引代表學生編號
*/
$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中的元素在拆分成組合后縱向出現的最大重復次數
$RepeatTime = $RepeatTime / count($StudentList);
$StartPosition = 1;
// 開始對每個班級的學生進行循環(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;
}
}
/* 打印結果 */
echo "<pre>";
print_r($Result);
?>
復制代碼 代碼如下:
<?php
/**
* 要解決的數學問題 :算出C(a,1) * C(b, 1) * ... * C(n, 1)的組合情況,其中C(n, 1)代表從n個元素里任意取一個元素
*
* 要解決的實際問題樣例:某年級有m個班級,每個班的人數不同,現在要從每個班里抽選一個人組成一個小組,
* 由該小組來代表該年級參加學校的某次活動,請給出所有可能的組合
*/
/* ################################### 開始計算 ################################### */
/**
* 需要進行排列組合的數組
*
* 數組說明:該數組是一個二維數組,第一維索引代表班級編號,第二維索引代表學生編號
*/
$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中的元素在拆分成組合后縱向出現的最大重復次數
$RepeatTime = $RepeatTime / count($StudentList);
$StartPosition = 1;
// 開始對每個班級的學生進行循環(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;
}
}
/* 打印結果 */
echo "<pre>";
print_r($Result);
?>
相關文章
php file_get_contents抓取Gzip網頁亂碼的三種解決方法
用 file_get_contents() 函數抓取網頁會發(fā)生亂碼現象。有兩個原因會導致亂碼,一個是編碼問題,一個是目標頁面開了Gzip,下面說的就是開了Gzip功能如何才能不亂碼的方法2013-11-11Yii2框架實現利用mpdf創(chuàng)建pdf文件功能示例
這篇文章主要介紹了Yii2框架實現利用mpdf創(chuàng)建pdf文件功能,結合實例形式分析了mpdf的下載、安裝及結合Yii2框架創(chuàng)建pdf文件的相關操作技巧,需要的朋友可以參考下2019-02-02解決laravel 5.1報錯:No supported encrypter found的辦法
這篇文章主要給大家介紹了關于解決laravel 5.1報錯:No supported encrypter found的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06