Home > Backend Development > PHP Tutorial > PHP N选M算法 大数组打印不出来

PHP N选M算法 大数组打印不出来

WBOY
Release: 2016-06-20 12:42:37
Original
1030 people have browsed it

function Combination($sort, $num){    $result = $data = array();    if( $num == 1 ) {        return $sort;    }    foreach( $sort as $k=>$v ) {        unset($sort[$k]);        $data   = Combination($sort,$num-1);        foreach($data as $row) {            $result[] = $v.','.$row;        }    }    return $result;}$starttime = explode(' ',microtime());$arr    = array(1,2,3,4,5,6,7,8,9,1,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33);//这个是组合$res        = Combination($arr,6);print_r($res);$endtime = explode(' ',microtime());$thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);$thistime = round($thistime,3);echo "本网页执行耗时:".$thistime." 秒";
Copy after login

我测试了好多,这个效率还可以33选5 1点多秒就出来了,但是选6的话,就出现500错误,我的是IIS7.5+PHP5.6 FAST-CGI模式!为啥会500错误,是不是数组太大,要调PHP.INI啊。哪个参数是


回复讨论(解决方案)

打开 php 的错误显示功能,你会看到 Fatal error: Allowed memory size of ...... 这样的错误
内存不够了!

考虑是给php分配的内存不够用,ini_set('memory_limit','500M')试下

不知道我理解的对不对
N选M,如果允许重复值,每次调用随机数键名对应的值就好
如果不允许重复值,使用shuffle()函数随机排列,然后直接取前M个元素

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template