美文网首页
Python河内之塔 汉诺塔问题

Python河内之塔 汉诺塔问题

作者: Cytosine | 来源:发表于2017-07-29 17:19 被阅读0次
    def hanoi(n, a, b, c):
        if n == 1 :
            print(a, '->', c)
        else:
            hanoi(n-1, a, c, b) # n-1个,从a到b
            print(a, '->', c)   # 第n个,从a到c
            hanoi(n-1, b, a, c) # n-1个,从b到c
    
    userInput = int(input('请输入层数:'))
    hanoi(userInput, 'A', 'B', 'C')
    

    相关文章

      网友评论

          本文标题:Python河内之塔 汉诺塔问题

          本文链接:https://www.haomeiwen.com/subject/ledclxtx.html