汉诺塔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))

你可能感兴趣的:(python主线学习,算法)