This article mainly introduces the method of solving the problem of N-Step step walking by Python, briefly describes the steps problem, and analyzes the relevant operation skills of Python using recursion and recursive algorithm to solve the step problem by the example, and the Friends can refer to the following
In this paper, we describe the method of Python solving the N-Step step-walk problem. Share to everyone for your reference, as follows:
Title: A building has an n-step staircase, the rabbit can jump 1, 2 or 3 steps each time, ask how many ways to go?
Analysis of Afanty:
In the face of this problem of law, self-propelled push is good, 1 order there are several ways to go? How many steps are there in order 2? How many steps are there in order 3? How many steps are there in order 4? How many steps are there in order 5?
Yes, it's the law!
Easy Wrong point: This is not a combinatorial problem, because the 1th time to go 1-order, 2nd-Step 2-step different from the 1th time 2-order, 2nd times to go 1-step
The following is the recursive implementation code for Python:
def allmethods (stairs): "' :p Aram stairs:the numbers of Stair : return: ' if Isinstance ( stairs,int) and stairs > 0: basic_num = {1:1,2:2,3:4} if stairs in Basic_num.keys (): return basic_num[ Stairs] Else: return Allmethods (stairs-1) + allmethods (stairs-2) + allmethods (stairs-3) else: print ' The num of stair is wrong ' return False
Of course it can be implemented in a non-recursive way, and here is the code based on the recursive method:
def allmethod (stairs): "' Recursive implementation :p Aram stairs:the amount of Stair : return: ' if Isinstance ( stairs,int) and stairs > 0: h1,h2,h3,n = 1,2,4,4 basic_num = {1:1,2:2,3:4} if stairs in Basic_num.keys (): C8/>return Basic_num[stairs] else: while n <= stairs: temp = h1 h1 = h2 h2 = h3 H3 = Temp + h1 + H2 return h3 else: print ' The num of stair is wrong ' return False
Well, these are the processes that are implemented using recursion and recursive methods respectively.