汉诺塔问题-简化版

题目来源:Python语言程序设计

授课老师: 嵩天、黄天羽、礼欣


结果显示


 代码

count = 0
def hanoi(n,src,dst,mid):
    global count
    if n == 1:
        print("{}:{}->{}".format(1,src,dst))
        count += 1
    else:
        hanoi(n-1,src,mid,dst)
        print("{}:{}->{}".format(n,src,mid,dst))
        count += 1
        hanoi(n-1,mid,dst,src)

hanoi(3,'A','B','C')
print("执行了:",count)

猜你喜欢

转载自blog.csdn.net/Mzjuser/article/details/82465628