# 6. 用递归方法求数列"1,1,1,3,5,9,17,31,..."的前20项
def ex06(n):
if n <= 3:
return 1
else:
return ex06(n-3) + ex06(n-2) + ex06(n-1)
A = [0] * 20
for i in range(1,21):
A[i-1] = ex06(i)
print(A)
程序缩进如图所示
结果如下:
[1, 1, 1, 3, 5, 9, 17, 31, 57, 105, 193, 355, 653, 1201, 2209, 4063, 7473, 13745, 25281, 46499]