Stairs (recursive) - Learning Algorithm

problem

Tree climbing stairs teacher, he can go every time Level 1 or Level 2, enter the staircase series,
find a different number of moves
such as: stairs, a total of three, he can always go one, or the first time to go a
level two second walk, you can go two for the first time, to go a second time, a
total of three methods.

Entry

Comprising a plurality of input lines, each containing a positive integer N, the representative series stairs,. 1
<= N <= 30 outputs the number of different moves, each row corresponding to an input line

Export

A different number of moves, each row corresponding to an input line of output

Sample input

5
8
10

Sample Output

8
34
89

Thinking

n = the walk stairs
after one go, n-1 + stage step moves
the two go, n-2-level step moves
f (n) = f (n -1) + f ( n-2)
boundary conditions: n-<0 0
n-0. 1 =

Code:


#include <iostream>
using namespace std;
int N;
int louti(int n)
{
if( n < 0)
return 0;
if( n == 0 )
return 1;
return louti(n-1) + louti(n-2);
}
int main()
{
while(cin >> N) {
cout << louti(N) << endl;
}
return 0;
}
Published 79 original articles · won praise 133 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_45822638/article/details/105028879