1、求阶乘
方法一: s = 1 sum = 0 for i in range(1,3): s = s*i sum += s print(s) print(sum) def fn(n): if n == 1: return 1 else: return fn(n-1) * n print(fn(3)) def fn(n): return 1 if n == 1 else fn(n-1) * n print(fn(3)) def fn1(n,a=1): if n == 1: return a a = a * n return fn1(n-1,a) print(fn1(3))
2、猴子吃桃
def foo(n=10): if n == 1: return 1 return (foo(n-1) + 1) * 2 def foo(n=1): if n == 10: return 1 return (foo(n + 1) + 1) * 2
原文:https://www.cnblogs.com/alrenn/p/12740660.html