Use "class" instead of "recursive method" and use PHP.

xiaoxiao2021-03-06  46

/ ************************** * Author: old Youth * email: wnadmin@sina.com * from: http: // blog. 9cbs.net/hahawen * reproduced please keep this part of this information, thank you *********************************** / question: a staircase has N steps How many kinds of top law each time, how many kinds of top laws are there, what is the steps of each way? This simple problem, our usual method is to write a recursive call, simply clear. However, here is achieved by the superposition of the class, although it is not much significant, but this design is still more, you can consider yourself.

// A stair has n steps, each time one or two steps, how many kinds of upper laws, what is the steps of each way to go. DEFINE ('TOTLE_STEP', 10); $ P = '; $ OBJ = New Step ($ P, 0, 0); $ OBJ-> Go ();

Class step {

Var $ parent; 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. '
';}}}?>

转载请注明原文地址:https://www.9cbs.com/read-81268.html

New Post(0)