# -*-coding:utf-8-*-
# 下楼问题,从楼上走到楼下共有h个台阶,每一步有三种走法:
# 走1个台阶,走2个台阶,走3个台阶。问有多少可走的方案。用递归思想和迭代思想编程。
# 递归
stack = [0] * 1024
steps = 0
num_of_method = 0
num_of_call = 0
def down_recursion(_high):
global steps, num_of_method, num_of_call
num_of_call += 1
if _high == 0:
num_of_method += 1
print "The %dth Way [Need %d Steps]:" % (num_of_method, steps)
for i in range(steps):
print stack[i],
print
return
if _high >= 1:
stack[steps] = 1
steps += 1
down_recursion(_high-1)
steps -= 1
if _high >= 2:
stack[steps] = 2
steps += 1
down_recursion(_high-2)
steps -= 1
if _high >= 3:
stack[steps] = 3
steps += 1
down_recursion(_high-3)
steps -= 1
down_recursion(5)
Python-下楼问题
最新推荐文章于 2023-11-12 16:14:26 发布