N-level stairs, one can be 1 or 2, ask how many different ways to go?
Solution:
If n represents the progression of a step, a n indicates that when a person walks to the nth step, all possible different methods are readily available:
(1) According to test instructions:
When N=1, it is obvious that only 1 kinds of cross-laws, namely a1=1.
When the n=2, you can step to the level of the cross, you can also step across level two,
Therefore, there are 2 different kinds of cross-laws, namely a2=2.
(2) by (1) Available:
If the first step crosses one level, then there are two steps left, and there are a2=2 (kinds) of cross-laws.
If the first step crosses two steps, then there is still a level of steps, there is a1=1 (kind) cross-law.
Therefore, a3=a1+a2=3
(3) When n=4, there are three kinds of situations discussed separately:
If the first step crosses one level, then there are three steps left, and there are a3=3 (kinds) of cross-laws.
If the first step crosses two steps, then there are two steps left, and there are a2=2 (kinds) of cross-laws.
So a4=a3+a2=5
(4) When n=5, there are 4 kinds of situations discussed separately:
If the first step crosses one level, then there are four steps left, and there are a4=5 (kinds) of cross-laws.
If the first step crosses two steps, then there are three steps left, and there are a3=3 (kinds) of cross-laws.
So a5=a3+a4=8
int func (int n) { if (1 = = n )return 1; Else if (2 = = n ) return 2;
return func (n-1) + func (n-2);}
The climbing steps of the algorithm