php FOR 循环优化有关问题

php中文网
发布: 2016-06-13 13:32:14
原创
1338人浏览过
php FOR 循环优化问题
上次发个帖子没人跟,可能是问题不具体或者php基础编程人少或者太忙.............

我想从1-33选出6个不重复的号,然后写入数据库
现在选号过程PHP处理的很慢还总出错误,谁有兴趣帮我把下面的代码优化下:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php
$str=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33);
for($i=0;$i<28;$i++){
    for($j=$i+1;$j<33;$j++){
        for($k=$j+1;$k<33;$k++){
            for($l=$j+1;$l<33;$l++){
                for($m=$l+1;$m<33;$m++){
                    for($n=$m+1;$n<33;$n++){
                        echo "$str[i]|$str[j]|$str[k]|$str[l]|$str[m]|$str[n]|<br>";//显示效果,本想写入TXT文档在传如数据库.
                    }
                }
            }
        }
    }
}
?>
登录后复制


------解决方案--------------------
PHP code
$a  = range(1, 33);
$ar = combination($a, 6);

//求组合高效率的10移动法
function combination($numArr,$combineLen) {
  $numCt    = count($numArr);
  if($combineLen &gt; $numCt) return;
  $bin    = str_pad('',$combineLen,'1');
  $bin    = str_pad($bin,$numCt,'0',STR_PAD_RIGHT);    
  $find    = $bin;
  $rs[]    = implode(' ',array_slice($numArr,0,$combineLen));
  $j        = 1;
  while(strrev($find) != $bin) {
    $k = explode('10',$find,2);
    $find = $find{0} === '0' ? strrev($k[0]).'01'.$k[1] : $k[0].'01'.$k[1];
    for($i=0;$i<font color="#e78608">------解决方案--------------------</font><br>
登录后复制
PHP code
[User:root Time:21:21:46 Path:/home/liangdong/php]$ php permutation.php 
Total time used : 9738642ms
[User:root Time:21:21:56 Path:/home/liangdong/php]$ cat permutation.php 
<?php $beg_time = microtime(true);
$beg_time *= 1000 * 1000;

$table = range(1, 33);
$temp = array();
$fp = null;

function _get_permutations($index, $num) {
        global $temp;
        global $total;
        global $table;
        global $fp;

        if ($num == $total) {
                global $perms;
                fwrite($fp, json_encode($temp) . PHP_EOL);
                return;
        }

        for ($ndx = $index; 33 - $ndx + $num >= $total; ++ $ndx) {
                $temp[$num] = $table[$ndx];
                _get_permutations($ndx + 1, $num + 1);
        }
}

function get_permutations($n = 6) {
        global $total;
        global $perms;
        global $fp;

        if ($n  33) {
                return false;
        }

        $total = $n;
        $fp = fopen("data.txt", "w");
        _get_permutations(0, 0);
        fclose($fp);
        return true;
}

get_permutations();

$end_time = microtime(true);
$end_time *= 1000 * 1000;
echo "Total time used : " . ($end_time - $beg_time) . "ms" . PHP_EOL;

?&gt; <div class="clear"></div>
登录后复制
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号