我看到這個algorithm that will take numbers or words and find all possible combinationsPHP採取一切組合
而且我使用它,但它不會返回所有的「真實」的組合。
PHP:
<?php
require_once 'Math/Combinatorics.php';
$words = array('cat', 'dog', 'fish');
$combinatorics = new Math_Combinatorics;
foreach($combinatorics->permutations($words, 2) as $p) {
echo join(' ', $p), "\n";
}
?>
,並返回:
cat dog
dog cat
cat fish
fish cat
dog fish
fish dog
但這些並不都是真正的組合,所有真正的組合,包括這些太:
cat cat
dog dog
fish fish
而這正是我需要,獲得所有真實組合的方法:
cat dog
dog cat
cat fish
fish cat
dog fish
fish dog
cat cat
dog dog
fish fish
你爲什麼不自己添加這些組合?看起來很簡單,可以循環瀏覽數據並手動添加對。 – 2012-03-20 12:49:51
這與以前的問題不一樣嗎?你似乎對答案有所懷疑。爲什麼不在那裏繼續? – Nanne 2012-03-20 12:52:16
Math_Combinatorics - 「返回給定集合和子集大小的所有組合和排列的包,保留關聯數組。」這裏的關鍵是「不重複」。 – strkol 2012-03-20 12:54:52