汉诺塔hanoi的python实现

上手python,练练手

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def hanoi(n,a,b,c):
    if n < 0:
        print('enter n >0')
    elif n==1:
        print(a,'-->',b)
    elif n > 0:
        hanoi(n-1,a,c,b)
        print(a,'-->',b)
        hanoi(n-1,b,a,c)

n = int(input('enter n= '))
a = (input('enter a= '))
b = (input('enter b= '))
c = (input('enter c= '))
print(hanoi(n,a,b,c))

    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/Lina_ACM/article/details/54177118
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞