Discuss / Python / 思考

思考

Topic source

岁羽谌谌

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

def move(n, a, b, c):

    if n == 1:

        print('move', a, '-->', c)

    else:

        move(n-1, a, c, b)#当大于1个的时候先把a移到b (小的那个移到b当中介)

        move(1, a, b, c)#把a移到b后,然后把a移到c(大的那个移到c当底子)

        move(n-1, b, a, c)#把a的两个都移走后,把小的那个(b)上的那个移回c上(把两个中小的b移到c上)

move(3, 'A', 'B', 'C')


  • 1

Reply