2019年4月25日 星期四

[UVa][ Volume I ] 100 - The 3n + 1 problem


UVa Volume I  100 - The 3n + 1 problem



題目來源: The 3n+1 problem


解題:


這題在UVa 題目中算是簡單題。

直接跟著題目的虛擬碼照做就可以了,也就是:

n = 3n + 1  , if n is odd

n = n / 2  , if n is even

使用一個計數器計算一共執行多少次


另外,這一題要注意的一點是 : 如果使用long型態則會有overflow問題

因此變數都使用了unsigned型態



參考解答(C++)


沒有留言:

張貼留言