Skip to content

Commit c0000fa

Browse files
committed
Solve #292
1 parent 101534d commit c0000fa

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,4 @@ mod n0283_move_zeroes;
220220
mod n0287_find_the_duplicate_number;
221221
mod n0289_game_of_life;
222222
mod n0290_word_pattern;
223+
mod n0292_nim_game;

src/n0292_nim_game.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)