Python汉诺塔作业

汉诺塔问题

n = int(input())
count = 0
def hanoi(n,a,b,c):
    global count
    if n == 1:
        print("将第{}个从{}搬到{}".format(1,a,c))
        count += 1
    else:
        hanoi(n-1,a,c,b)
        print("将第{}个从{}搬到{}".format(n,a,c))
        count += 1
        hanoi(n-1,b,a,c)
hanoi(n,'A','B','C')
print("共{}步".format(count))

输出

 

posted @ 2020-03-30 17:36  玩的三立方  阅读(309)  评论(0编辑  收藏  举报