首页 >后端开发 >php教程 >求php多数组组合排列算法

求php多数组组合排列算法

WBOY
WBOY原创
2016-06-23 13:32:041055浏览

模拟四个数组:
$list [‘a’] = array (1,2,3);
$list [‘b’] = array (4,5,6);
$list [‘c’] = array (7,8,9);
 
要求组合成这样:14,15,16,17,18,19,24,25,26,27,28,29,34,35,36,37,38,39,47,48,49,57,58,59,67,68,69

求一算法 


回复讨论(解决方案)

网上看到一段代码:

<?php    class sufa{        public function main(){            $list [‘a’] = array (1,2,3);            $list [‘b’] = array (1,2);            $list [‘c’] = array (1,2,3,4);    //      $list [‘f’] = array (1,2,3,4,5);    //      $list [‘d’] = array ("+","-","*","/","%");                      foreach($list[‘a’] as $v){                $this->getsulie($list,$v,1);            }                     }        function getsulie($list,$content,$deep){            $i=0;            if($deep>count($list)){                return;            }            foreach($list as $k=>$v){                if($i==$deep){                    foreach($list[$k] as $vv){                        $vv = $content.$vv;                        if($deep==count($list)-1){                            echo $vv."<br/>";                        }else {                            $this->getsulie($list,$vv,$deep+1);                        }                    }                    break;                }                $i++;            }            return;        }    }    $s = new sufa();    $s->main();     ?>


但是跟这个不太一样

递归不太会用,求大神指点

$list['a'] = array (1,2,3);$list['b'] = array (4,5,6);$list['c'] = array (7,8,9);foreach($list as $key=>$row) {  unset($list[$key]);  foreach($row as $col) {    foreach($list as $r)      foreach($r as $v) $res[] = $col . $v;  }}echo join(',', $res);
14,15,16,17,18,19,24,25,26,27,28,29,34,35,36,37,38,39,47,48,49,57,58,59,67,68,69

$list['a'] = array (1,2,3);$list['b'] = array (4,5,6);$list['c'] = array (7,8,9);foreach($list as $key=>$row) {  unset($list[$key]);  foreach($row as $col) {    foreach($list as $r)      foreach($r as $v) $res[] = $col . $v;  }}echo join(',', $res);
14,15,16,17,18,19,24,25,26,27,28,29,34,35,36,37,38,39,47,48,49,57,58,59,67,68,69




正解,那需要3个数字组合的时候,又该如何额实现,比如

	$list['a'] = array (1,2,3);	$list['b'] = array (4,5,6);	$list['c'] = array (7,8,9);	$list['d'] = array (10,11,12);


最终需要得到:
147,148,149,157,158,159,167,168,169,247,248,249,257,258,259,267,268,269,347,348,349,357,358,359,367,368,369

$list['a'] = array (1,2,3);$list['b'] = array (4,5,6);$list['c'] = array (7,8,9);foreach($list as $key=>$row) {  unset($list[$key]);  foreach($row as $col) {    foreach($list as $r)      foreach($r as $v) $res[] = $col . $v;  }}echo join(',', $res);
14,15,16,17,18,19,24,25,26,27,28,29,34,35,36,37,38,39,47,48,49,57,58,59,67,68,69





上面写错了,多写了一个数组,
	$list['a'] = array (1,2,3);	$list['b'] = array (4,5,6);	$list['c'] = array (7,8,9);


最终需要得到:
147,148,149,157,158,159,167,168,169,247,248,249,257,258,259,267,268,269,347,348,349,357,358,359,367,368,369

本以为你是工作上遇到问题急需解决,没承想你是在研究算法
从算法角度看,你的两个题目都应是:对原始数据求组合( C(m,n) )的结果做 笛卡尔积 运算
代码就不贴了,你可以在精华区找到多个版本

本以为你是工作上遇到问题急需解决,没承想你是在研究算法
从算法角度看,你的两个题目都应是:对原始数据求组合( C(m,n) )的结果做 笛卡尔积 运算
代码就不贴了,你可以在精华区找到多个版本



谢谢
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn