Discuss / Python / 没搞懂

没搞懂

Topic source
def hanoi(n,a,b,c):
    if n==1:
        print(a,'-->',c)
    else :
        hanoi(n-1,a,c,b)
        hanoi(1,a,b,c)
        hanoi(n-1,b,a,c)
print('please enter the level of hanoi: ')
I=int(input())
hanoi(I,'A','B','C')

你得想通递归函数到底是个什么玩意,递归就是函数在定义中使用函数自身的方法。

这么一想你就能明白了,在定义函数的过程中,我们调用它自身。

我定义一个函数,它要达到什么样的效果,那么我在定义函数的时候,我就默认为它已经有这个效果了,所以它就有这效果了


  • 1

Reply