好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python解决N阶台阶走法问题的方法

这篇文章主要介绍了Python解决N阶台阶走法问题的方法,简单描述了走台阶问题,并结合实例形式分析了Python使用递归与递推算法解决走台阶问题的相关操作技巧,需要的朋友可以参考下

def allMethods(stairs):
  '''''
  :param 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 
def allMethod(stairs):
  '''''递推实现
  :param 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():
      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 

以上就是Python解决N阶台阶走法问题的方法的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python解决N阶台阶走法问题的方法的详细内容...

  阅读:40次