First, let's take a look at the following staircase:
Altogether four layers, we stipulated " one person climbs the staircase, one step can take one level, the second level step, if the staircase has n level, asks to write the procedure, asks altogether how many kinds of Go method ", Next we statistics this four layer, each layer's Walk method:
From this short statistic, we can find a law (if it is not seen, can be more than a few stairs), starting from the third ladder, the way is the first step and the second step of the law of the sum, the fourth step is the third ladder and the second step of the law of the sum, so we can get the following formula:
The recursive algorithm is used here.
And the first ladder and the second ladder such an irregular number, we can quantify him, thus, we come to the following implementation of the method to find the steps of the N-step:
function getnum ($n) { switch($n) { Case 1: return 1; Case 2: return 2; default: return getnum ($n-1) + getnum ($n-2);} }
If climbing stairs can also step three steps, this time is actually, N-class stair problem can be divided into: N-1 class staircase, N-2 stairs, N-3 stairs of the way of the sum. And so on, but generally it is the biggest three steps.
Stair-taking algorithm