close
import time

starttime = time.time()

def hano(n,A,B,C):
    if n==1:
        print(f"put {n} from {A} to {C}")
    else:
        hano(n-1,A,C,B)
        print(f"put {n} from {A} to {C}")
        hano(n-1,B,A,C)

n= int(input("請輸入圈數:"))
hano(n,"A","B","C")
tend=time.time()
print(f"It cost {tend-starttime} sec")

 

參考 http://kailotus.blogspot.com/2018/05/python22hanoi.html 寫的河內塔

後面加了一條計算運算時間

原本以為自己電腦不錯可以跑出傳說中的64層河內塔

沒想到是我大意了

2^64-1

一秒走個一萬步

1.8446744e+15  秒才能跑完 =21350398233.5 天= 58494241.7355

謝謝大家

看來我這輩子用這台破電腦是看不到世界毀滅了


arrow
arrow

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()