漢諾塔(hanoi)

漢諾塔代碼:spa

def hanoi(n,x,y,z):
    if n == 1:
        print(x,'-->',z)
    else:
        hanoi(n-1,x,z,y)
        print(x,'-->',z)
        hanoi(n-1,y,x,z)
        
n = int(input('Input your number:'))
hanoi(n,'X','Y','Z')
相關文章
相關標籤/搜索