deffuna(n):print("I am yegeli")deffuncb(n):
funa(100)print("I am wangxiaojing")
funcb(100)
I am yegeli
I am wangxiaojing
# fun_a 表示计算阶乘# 利用数学公式deffun_a(n):print(n)# 递归一定要有结束条件if n ==1:return1return n * fun_a(n-1)
rst = fun_a(5)print("f(5) = ",rst)
5
4
3
2
1
f(5) = 120
# 递归必须有结束条件,否则会死掉deffun_b(n):print(n)return n * fun_b(n-1)#fun_b(100)
# 斐波那契额数列# f(n)=f(n-1)+f(n-2),,n>3# f(n)=1,,,,n=1,2deffib(n):if n ==1or n ==2:return1return fib(n-1)+ fib(n-2)
rst = fib(10)print("rst=",rst)
rst= 55
# 汉诺塔# A B C A->C# 1# A->C# 2# A->B A->C B->C# 3# A->C A->B C->B A->C B->A B->C A->C
a ='A'
b ='B'
c ='C'defhano(a,b,c,n):if n ==1:print("{}-->{}".format(a,c))returnNoneif n ==2:print("{}-->{}".format(a,c))print("{}-->{}".format(a,b))print("{}-->{}".format(b,c))returnNone
hano(a,c,b,n-1)print("{}-->{}".format(a,c))
hano(b,a,c,n-1)