2013年6月8日

实用的任选n算法

摘要: $arr = array(1,2,3,4,5,6)需求从$arr中选出任意多个元素的所有组合比如任选三,通常我们只需要用三个for循环,但是这并不能适合于所有情况,如果在要求得出所有任选四的所有组合,就必须再加四个for循环,如果再多呢?适用于任选n的所有情况:<?php//核心:采用堆+递归的形式实现function get_rx($target,$arr) { static $rzRzt = array(); $count = count($arr); $selfName = __FUNCTION__; global $n; if ($count<1)... 阅读全文

posted @ 2013-06-08 09:52 mtima 阅读(211) 评论(0) 推荐(0) 编辑

二维组合连乘相加

摘要: function award_sp_cal($spRzt) { $totalAward = 1; $temp = null; foreach($spRzt as $sp) { if (count($sp) == 1) { $totalAward *= array_shift($sp); } else if (count($sp) >1 ) { $temp = $totalAward; $totalAward = 0;//原理: a*x + b*x = (a+b)*x ... 阅读全文

posted @ 2013-06-08 09:35 mtima 阅读(158) 评论(0) 推荐(0) 编辑

导航