青蛙只能跳1步或者2步
当台阶数为1或2时,总方法数为台阶数,返回即可
当台阶数大于2就要使用递归了
使用了斐波那契数 代码如下
def frog(n):
if n <= 2:
return n
step_1, step_2 = 1, 2
for i in range(3, n+1):
step_1, step_2 = step_2, step_1+step_2
return step_2
n = int(input("请输入台阶数:"))
print(frog(n))
结果如下