You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
Java 語言實現:
public static boolean canWinNim(int n) {
if (n % 4 == 0)
return false;
return true;
}
function canWinNim(nums)
assert(type(nums) == "number", "arguments num is not a number")
return nums % 4 ~= 0
end
print(canWinNim(1))
- 非是 ~,而不是 Java 中的 !
- Lua 動態類型語言,數值纔是有類型的,變量無類型(Java 傳入參數 int n,而 lua 直接 nums)