问题的关键是要理解递归思想
代码
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)
n=eval(input("请输入递归层数:"))
move(n,'A','B','C')
输入n=5输出
A -> C
A -> B
C -> B
A -> C
B -> A
B -> C
A -> C
A -> B
C -> B
C -> A
B -> A
C -> B
A -> C
A -> B
C -> B
A -> C
B -> A
B -> C
A -> C
B -> A
C -> B
C -> A
B -> A
B -> C
A -> C
A -> B
C -> B
A -> C
B -> A
B -> C
A -> C