Discuss / Python / 敲好玩!写得敲开心!

敲好玩!写得敲开心!

Topic source

ayaka_neko

#1 Created at ... [Delete] [Delete and Lock User]
def hanoi(n, A, B, C):
    if n == 1: 
        print (A, '-> ', C)
        return None
    else: 
        hanoi(n - 1, A, C, B)
        print (A, '-> ', C)
        hanoi(n - 1, B, A, C)
def power(n):
    p = 1
    while n>0:
        n = n-1
        p = p * 2
    return p
n = int(input('please input the number of the disk: \n'))
hanoi(n, 'A', 'B','C')
m = power(n) - 1
print ('It takes at least', m,'steps to move all the disks from rod A to rod C.')

  • 1

Reply