我在哪里可以找到一个组合算法(NCR)在PHP? [英] Where can I find a combinations algorithm(nCr) in PHP?
问题描述
通过组合算法,我的意思是code,将产生类似的输出下面的例子。
By combinations algorithm I mean code that will produce output similar to the example below.
我发现C实现由高德纳了,但是它采用的是GOTO语句。 PHP 5.2.4(PHP我必须使用的版本)不支持转到。所以,当我将它移植到PHP会报错在转到statments。我会进一步修改,但我想我可能会失去效率。
I found the C implementation by Knuth already but it used the "goto" statement. PHP 5.2.4 (the version of PHP I must use) does not support "goto". So when I port it to PHP it will error on the goto statments. I could modify it further but I think I might loose efficiency.
或者,如果没有一个PHP实现或许是不太面向对象的实现(以使得实际算法不隐藏)或不使用转到的实现。
Or if not a PHP implementation perhaps an implementation that is not too object-oriented(so that the actual algorithm is not hidden) or an implementation that does not use "goto".
例: 无碳复写纸,N = 3,R = 2
Example: nCr, n=3, r=2
3C2 =
{1,2},{1,3},{2,3}
{1,2},{1,3},{2,3}
推荐答案
也许你可以找到<一href="http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n">here, <一href="http://stackoverflow.com/questions/4279722/php-recursion-to-get-all-possibilities-of-strings">here或点击这里 的一些解决方案。
Maybe you can find here, here or here some solutions.
这篇关于我在哪里可以找到一个组合算法(NCR)在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!