首页 >后端开发 >php教程 >哪位高手帮小弟我写个函数方法

哪位高手帮小弟我写个函数方法

WBOY
WBOY原创
2016-06-13 10:19:18758浏览

谁帮我写个函数方法
最近在研究彩票的问题,有一个关于复式数的分解问题始终在困扰我。

例子

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/**比如4个号码分解成3个号码和2个号码4码:[1,2,3,4] 一组号码分解成3码后:[1,2,3] [1,2,4] [1,3,4] [2,3,4] 四组三位数的号码分解成2码后:[1,2] [1,3] [1,4] [2,3] [2,4] [3,4] 六组二位数的号码不考虑位置又如5个号码分解成4个号码4码:[1,2,3,4,5] 一组号码分解成4码后:[1,2,3,4] [1,2,3,5] [1,2,4,5] [1,3,4,5] [2,3,4,5]四组四位数的号码分解成3码后:[1,2,3] [1,2,4] [1,2,5] [1,3,4] [1,3,5] [1,4,5] [2,3,4] [2,3,5] [2,4,5] [3,4,5] 十组三位数的号码分解成2码后:[1,2] [1,3] [1,4] [1,5] [2,3] [2,4] [2,5] [3,4] [3,5] [4,5] 十组二位数的-------------------------------------------------大家应该看出规律了吧谁能帮我写个方法array lottery(array $num, int $i){}@param $num 是指一组数字@param $i 是指分解的码数返回结果数组如$arr1 = lottery(array(1,2,3,4,5), 4);$arr2 = lottery(array(1,2,3,4,5), 3);此时$arr1 的值应该是一个数组[1,2,3,4] [1,2,3,5] [1,2,4,5] [1,3,4,5] [2,3,4,5]而$arr2 的值也是一个数组分解成3码后:[1,2,3] [1,2,4] [1,2,5] [1,3,4] [1,3,5] [1,4,5] [2,3,4] [2,3,5] [2,4,5] [3,4,5]这个问题我反复研究过,貌似挺难的。。过年了,给66分,祝答题者六六大顺*/


------解决方案--------------------
我复制贴过来吧, 这个实现还是比较高效的算法.
PHP code
function Combination($arr, $size = 1) {     $len = count($arr);     $max = pow(2,$len) - pow(2,$len-$size);    $min = pow(2,$size)-1;      $r_arr = array();     for ($i=$min; $i';print_r($r); echo '
';/*Array( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 4 ) [2] => Array ( [0] => 1 [1] => 3 [2] => 4 ) [3] => Array ( [0] => 2 [1] => 3 [2] => 4 ) [4] => Array ( [0] => 1 [1] => 2 [2] => 5 ) [5] => Array ( [0] => 1 [1] => 3 [2] => 5 ) [6] => Array ( [0] => 2 [1] => 3 [2] => 5 ) [7] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [8] => Array ( [0] => 2 [1] => 4 [2] => 5 ) [9] => Array ( [0] => 3 [1] => 4 [2] => 5 ))*/
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn