-2

質問したいのですが、php 関数 fibonacci の php コードを再帰的に作成する方法を教えてください。「for」ループなし。このようなコードがありますが、「for」なしではどうですか? みんなありがとう、、

<?php function fibo($n){
if($n==0)
    return 0;
elseif($n==1)
    return 1;
else
    $tambah=fibo($n-1)+fibo($n-2);
    return $tambah;
    }

for($n=0;$n<15;$n++){
    echo fibo($n)."<br/>";}
4

1 に答える 1

1
function repeat($func, $times) {
    if ($times <= 0) {
        return;
    }
    $func($times);
    repeat($func, $times - 1);
}

$f = function($n) {
    echo fibo($n)."<br/>";
};

repeat($f, 15);

デモhttp://codepad.viper-7.com/F9TKPS

于 2012-11-25T00:37:54.240 に答える