|
| 1 | +/** |
| 2 | + * [292] Nim Game |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * |
| 6 | + * 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. |
| 7 | + * |
| 8 | + * Example: |
| 9 | + * |
| 10 | + * |
| 11 | + * Input: 4 |
| 12 | + * Output: false |
| 13 | + * Explanation: If there are 4 stones in the heap, then you will never win the game; |
| 14 | + * No matter 1, 2, or 3 stones you remove, the last stone will always be |
| 15 | + * removed by your friend. |
| 16 | + */ |
| 17 | +pub struct Solution {} |
| 18 | + |
| 19 | +// submission codes start here |
| 20 | + |
| 21 | +impl Solution { |
| 22 | + pub fn can_win_nim(n: i32) -> bool { |
| 23 | + n % 4 != 0 |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// submission codes end |
| 28 | + |
| 29 | +#[cfg(test)] |
| 30 | +mod tests { |
| 31 | + use super::*; |
| 32 | + |
| 33 | + #[test] |
| 34 | + fn test_292() { |
| 35 | + assert_eq!(Solution::can_win_nim(4), false); |
| 36 | + } |
| 37 | +} |
0 commit comments