有需要的同學直接Ctrl+C and Ctrl+V,函數中pos參數個數就是你有幾個塔,從而可以推廣至N個鐵柱的解
def Hanoid(n,posStart,posMiddle,posEnd): if n ==1 : print("move %d from %s to %s"%(n,posStart,posEnd)); return ; Hanoid(n-1,posStart,posEnd,posMiddle); print("move %d from %s to %s" % (n, posStart, posEnd)); Hanoid(n-1,posMiddle,posStart,posEnd); Hanoid(10,"小鐵柱A","小鐵柱B","小鐵柱C")