汉诺塔递归算法 python实现

数据结构复习内容--栈的应用 汉诺塔



def hannuota(n:int,a:str,b:str,c:str):
    if n==1:
        print(a,"--->",c)
    else:
        hannuota(n-1,a,c,b)#n-1个圆盘从a挪到b,但是绕过c
        print(a,"--->",c)#将a直接挪到c
        hannuota(n-1,b,a,c)#n-1个圆盘从b挪到c,但是绕过a

if __name__ == '__main__':
    n=int(input("请输入个数:"))
    hannuota(n,'a','b','c')

你可能感兴趣的:(python,数据结构)