Python-下楼问题

来源:互联网 发布:wan微型端口 编辑:程序博客网 时间:2024/05/16 12:14
# -*-coding:utf-8-*-# 下楼问题,从楼上走到楼下共有h个台阶,每一步有三种走法:# 走1个台阶,走2个台阶,走3个台阶。问有多少可走的方案。用递归思想和迭代思想编程。# 递归stack = [0] * 1024steps = 0num_of_method = 0num_of_call   = 0def down_recursion(_high):global steps, num_of_method, num_of_callnum_of_call += 1if _high == 0:num_of_method += 1print "The %dth Way [Need %d Steps]:" % (num_of_method, steps)for i in range(steps):print stack[i],print returnif _high >= 1:stack[steps] = 1steps += 1down_recursion(_high-1)steps -= 1if _high >= 2:stack[steps] = 2steps += 1down_recursion(_high-2)steps -= 1if _high >= 3:stack[steps] = 3steps += 1down_recursion(_high-3)steps -= 1 down_recursion(5)

0 0
原创粉丝点击