def compute(x):
if x<10:
return x*compute(x*2)
else:
return 1
print(compute(1))
我知道这是某种递归函数,但是如果有人对如何解决此任务有很好的解释,我将不胜感激。
答案 0 :(得分:0)
这是计算:
compute(1) =>
(1 * compute(2)) => // since 1 < 10
(1 * (2 * compute(4))) => // since 2 < 10
(1 * (2 * (4 * compute(8)))) => // since 4 < 10
(1 * (2 * (4 * (8 * compute(16))))) => // since 8 < 10
(1 * (2 * (4 * (8 * 1)))) => // since 16 >= 10
64 // simple calculation
这是您要找的吗?