php 如何计算最大值 可能的组合数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3425992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate the max. number of combinations possible?
提问by Click Upvote
Possible Duplicates:
Display possible combinations of string
algorithm that will take numbers or words and find all possible combinations
If I have 3 strings, such as:
如果我有 3 个字符串,例如:
"abc def xyz"
And I want to find the max number of combinations I can generate by rearranging these strings, e.g:
我想通过重新排列这些字符串来找到我可以生成的最大组合数,例如:
- abc xyz def
- def xyz abc
- xyz abc def
- abc xyz 定义
- def xyz abc
- xyz abc def
etc. What's the formula/algorithm for calculating this?
等等。计算这个的公式/算法是什么?
回答by Damian Leszczyński - Vash
This is not a combination but permutation. The algorithm is n!where n is the number of elements.
这不是组合而是排列。算法是n!其中 n 是元素的数量。
Why ?
为什么 ?
Because you have 3 values to place on three places, so for the first place You have three option, for second only two (becuase You already palace first string) and at the end You have only one option.
因为你有 3 个值可以放在三个地方,所以对于第一个你有三个选项,第二个只有两个(因为你已经在第一个字符串中),最后你只有一个选项。
3 * 2 * 1 = 3! = 6
3 * 2 * 1 = 3!= 6
But if You can repeate those chooses than you have permutation with repetition
但是,如果您可以重复这些选择,那么您就可以重复排列
So for first place You can chose from 3 string and for the second also and so one
因此,对于第一名,您可以从 3 个字符串中进行选择,第二个也可以选择一个等等
3 * 3 * 3 = 3^3 = 27
3 * 3 * 3 = 3^3 = 27
n^k - where n is the number of strings and k - the number of "locations"
n^k - 其中 n 是字符串的数量,k - “位置”的数量
And the code algorithm look like this:
代码算法如下所示:
function fact($n)
{
if ($n == 0)
{
return 1;
}
else
{
return $n * fact($n - 1);
}
}
This is a recursive example
这是一个递归的例子
回答by corymathews
If I remember correctly it's n! combinations.
如果我没记错的话是n!组合。
so for 9 you would have
所以对于 9 你会有
9*8*7*6*5*4*3*2 = 362880 combinations
9*8*7*6*5*4*3*2 = 362880个组合
回答by Jim W.
Look into permutations. O'Reilley has some good information on this via google. If I have some extra time, I will try and draft up an example for you.
看看排列。O'Reilley 通过谷歌提供了一些很好的信息。如果我有一些额外的时间,我会尝试为您起草一个示例。
Update
更新
Here is some code, not 100% if it works correctly, but you should be able to modify it to your needs (the core code came from the O'Reilley site, fyi):
这是一些代码,如果它工作正常,则不是 100%,但您应该能够根据需要对其进行修改(核心代码来自 O'Reilley 站点,仅供参考):
<?php
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
print join(' ', $perms) . "\n";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
pc_permute(array('abc', 'xyz', 'def', 'hij'));
?>
EDIT
编辑
Just saw that he wanted the algorithm, either or the code should produce the results for other lurkers :) See other answers for the algorithm, which is n!
刚刚看到他想要算法,或者代码应该为其他潜伏者产生结果:) 看到算法的其他答案,这是n!
回答by dewalla
3*2*1= 6 factorial!
3*2*1= 6 阶乘!
3 strings = 6 combinations..... 4 strings = 24 combinations.....etc
3 个字符串 = 6 个组合..... 4 个字符串 = 24 个组合..... 等