php类的举例,用类来代替递归方法,用php举例_php _ 搞代码
問題:一個(gè)樓梯有n個(gè)臺(tái)階,每次上一個(gè)或兩個(gè)臺(tái)階,共有多少種上法, 每種走法的步驟是什么樣的?
這個(gè)簡單問題,我們通常的方法是寫一個(gè)遞歸調(diào)用,簡單明了。但是,這里通過類的疊加來實(shí)現(xiàn),雖然本身沒有太大的意義,但是這種設(shè)計(jì)的用途還是滿多的,可以自己考慮考慮。
//一個(gè)樓梯有n個(gè)臺(tái)階,每次上一個(gè)或兩個(gè)臺(tái)階,共有多少種上法, 每種走法的步驟是什么樣的.
define('TOTLE_STEP', 10);
$p = '';
$obj = new step($p, 0, 0);
http://www.gaodaima.com/46654.html用“類”來代替“遞歸方法”,用php舉例_php
$obj->go();
class step{
var $parent;
var $count;
var $step;
var $son1;
var $son2;
function step(&$parent, $step, $count){
$this->parent = &$parent;
$this->step = $step;
$this->count = $count + $step;
}
function go(){
if($this->count==TOTLE_STEP)
$this->callback();
if($this->count<=TOTLE_STEP-1){
$this->son1 = new step($this, 1, $this->count);
$this->son1->go();
}
if($this->count<=TOTLE_STEP-2){
$this->son2 = new step($this, 2, $this->count);
$this->son2->go();
}
}
function callback($str=''){
if($this->parent!=null){
$str = $this->step.$str;
$this->parent->callback('--'.$str);
}else{
echo $str.'
';
}
}
}
?>
歡迎大家閱讀《用“類”來代替“遞歸方法”,用php舉例_php》,跪求各位點(diǎn)評(píng),若覺得好的話請(qǐng)收藏本文,by 搞代碼
微信 賞一包辣條吧~
支付寶 賞一聽可樂吧~
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php类的举例,用类来代替递归方法,用php举例_php _ 搞代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux脚本算术函数,Linux基础之
- 下一篇: 动态规划算法php,php算法学习之动态