递归实例应用二:求阶乘
def factorial(x):
if x == 1: #出口是1!=1
return 1
else:
return x * factorial(x-1)
print(factoria(input())
递归实例应用三:斐波那契
def fib(x):
if x ==0 or x ==1: #首两位决定后面数值
return 1
else:
return fib(x-1)+fib(x-2)
print(input())
递归实例应用四:求乘方
def power (x,y):
if y == 0: # 出口 :x^0 =1
return 1
else:
return x * power (x, y-1)
print(power(3,3))
递归实例应用五:判断正数奇偶
def is_even(x):
if x == 0:
return True
else:
return is_odd(x-1)
def is_odd(x):
return not is_even(x)
print(is_odd(58))
print(is_even(22))