久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      如何用PHP迭代器來(lái)實(shí)現(xiàn)一個(gè)斐波納契數(shù)列

      斐波納契數(shù)列通常做法是用遞歸實(shí)現(xiàn),當(dāng)然還有其它的方法。這里現(xiàn)學(xué)現(xiàn)賣(mài),用PHP的迭代器來(lái)實(shí)現(xiàn)一個(gè)斐波納契數(shù)列,幾乎沒(méi)有什么難度,只是把類(lèi)里的next()方法重寫(xiě)了一次。

      注釋已經(jīng)寫(xiě)到代碼中,也是相當(dāng)好理解的。

      /** * @author 簡(jiǎn)明現(xiàn)代魔法 http://www.nowamagic.net */ class Fibonacci implements Iterator {      private $previous = 1;      private $current = 0;      private $key = 0;           public function current() {          return $this->current;      }           public function key() {          return $this->key;      }           public function next() {  // 關(guān)鍵在這里 // 將當(dāng)前值保存到  $newprevious         $newprevious = $this->current;  // 將上一個(gè)值與當(dāng)前值的和賦給當(dāng)前值         $this->current += $this->previous;  // 前一個(gè)當(dāng)前值賦給上一個(gè)值         $this->previous = $newprevious;          $this->key++;      }           public function rewind() {          $this->previous = 1;          $this->current = 0;          $this->key = 0;      }           public function valid() {          return true;      }  }  $seq = new Fibonacci;  $i = 0;  foreach ($seq as $f) {      echo "$f ";      if ($i++ === 15) break;  }

      程序運(yùn)行結(jié)果:

      0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

      推薦:《PHP教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)