【发布时间】:2015-08-14 10:49:05
【问题描述】:
给定一个函数如下:f(n) = f(n-1) + f(n-3) + f(n-4)
f(0) = 1
f(1) = 2
f(2) = 3
f(3) = 4
我知道在一个函数中使用三个递归调用来实现它。但我想在函数内只使用一个递归调用来做到这一点。怎么做?
这里使用 3 个递归调用来实现是我的代码:
def recur(n):
if n == 0:
return 1
elif n == 1:
return 2
elif n == 2:
return 3
elif n == 3:
return 4
else:
return recur(n-1) + recur(n-3) + recur(n-4) #this breaks the rule because there are 3 calls to recur
【问题讨论】:
标签: python python-2.7 python-3.x recursion