PHP 求值问题
巴扎黑
巴扎黑 2017-04-11 09:03:20
[PHP讨论组]

假定 x =4;
输出 8 9 16 17 18 19 32 33 34 35 ... (数值在100以内)

x = 4;
a   b     c   d     e   f     g   h     i   j     ...
2x  2x+1  2a  2a+1  2b  2b+1  2c  2c+1  2d  2d+1  ...
8   9     16  17    18  19    32  33    34  35
巴扎黑
巴扎黑

全部回复(2)
怪我咯
function f(n)
{
    var x =4;
    if(n==1){
        return 2*x;
    }
    if(n==2){
        return f(n-1)+1
    }
    if(n%2 == 0){
        return f(n-1)+1;
    }else{
        return 2*f((n-1)/2)
    }
}
巴扎黑
function g($start = 0, $maxValue = 100)
{
    if ($start < 0)
        return false;

    $data = [$start];
    $i = 0;
    $index = 1;
    while ($data[$i] <= $maxValue) {
        $i++;
        if ($i % 2 == 0) {
            $data[$i] = $data[$i - 1] + 1;
        } else {

            if ($i == 1) {
                $data[$i] = $data[$index - 1] * 2;
            } else {
                $data[$i] = $data[$index] * 2;
                $index++;
            }
        }
        if ($data[$i] > $maxValue)
            break;

        echo $data[$i], '     ';
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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