Recursive - similar to climbing stairs frog jump

The recursive problem into smaller sub-scale problem solving

Example: stair climbing
tree climbing stairs teacher, he can go every time Level 1 or Level 2, enter the staircase series, find a few different moves
such as: stairs, a total of three, he can always go one, or The first to go a second time to go two, can go two for the first time, go a second time, a total of three methods.
Input
Input contains a number of rows, each row 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

Stairs
output
a different number of moves, each row corresponding to an input line of output
sample input
. 5
. 8
10
sample output
. 8
34 is
89

Problem-solving ideas: the n of steps after the walk go = a, n-1 + stage moves go step two, n-2 and the walk stairs
recursive expression is: F (n) = f (n-1) + f
(n-2) boundary conditions: n = 1,1 species moves, n = 2,2 species moves

 

python code:

 

"""
list = ["a","b","c","d","e","f"]
i = 1
for i,j in enumerate(list[i+1:],1):
    #list[2] = "k"
    #if (i < len(list)):
        #print(i,list[i])
    print(i,j)

The recursive problem into smaller sub-scale problem solving
Example: stairs
Tree climbing stairs teacher, he can go every time Level 1 or Level 2, the input series of stairs, find a different number of moves
For example: There are three stairs, he could have gone every level, or go first level, the second to go two, can go two for the first time, take a second, a
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
Climbing stairs
The number of different output moves, each row corresponding to an input line of output
Sample input
5
8
10
Sample Output
8
34
89

In some places a similar topic frog jump, the principle is the same
"""

def stairs(n):

    if (n == 1):
        return 1
    if (n == 2):
        return 2
    return stairs(n-1)+stairs(n-2)

def main():
    stepNum = int (the INPUT ( " Please enter the number of steps: " ))
    result = stairs(stepNum)

    Print ( " the number of steps is:!% d% d has the kind of moves " % (stepNum, the Result))

if (__name__ == "__main__"):
    main()

 

Guess you like

Origin www.cnblogs.com/an-wl/p/12383286.html