Discuss / Python / 尾递归汉诺塔

尾递归汉诺塔

Topic source

H

#1 Created at ... [Delete] [Delete and Lock User]
# if n == 1, then move a to c directly
# if n > 1, then first consider how to move the first n-1 from a to b
# second is to move the last 1 from a to c
# The third also the last step is to move n-1 from b to c
def move(n,a,b,c):
    if n == 1:
        print(a,'-->',c)
    else:
        move(n-1,a,c,b)
        move(1,a,b,c)
        move(n-1,b,a,c)

ksahgfiwhi

#2 Created at ... [Delete] [Delete and Lock User]

我运行这个代码为什么结果的末尾会有个None呢?怎么去掉这个None?

A --> C
A --> B
C --> B
A --> C
B --> A
B --> C
A --> C
None

  • 1

Reply