汉诺塔不必多说,常用的实现方式——递归 也不用多说,直接上代码:
__author__ = "Jazzon"
__coding__ = "UTF-8"
__version__ = "python3.x"
__contact__ = "1299277143@qq.com"
A = [6, 5, 4, 3, 2, 1]
B = []
C = []
n = len(A)
def move(n, A, B, C):
if n == 1:
C.append(A.pop())
else:
move(n - 1, A, C, B)
C.append(A.pop())
move(n - 1, B, A, C)
def main():
print("移动前:")
print('A = ', A, '\n', 'C = ', C, sep='')
move(n, A, B, C)
print("移动后:")
print('A = ', A, '\n', 'C = ', C, sep='')
if __name__ == '__main__':
main()