292. Nim Game
问题: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.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
思考:4的倍数,所以如果n/4! = 0; 则可以确定你能拿到最后那个石头。但是存在漏洞。。。前期你该如何取石块。感觉不是很清晰。
解决:
1 package com.wang.test; 2 3 /* @tittle:292. Nim Game 4 * @auther:wwwglin 5 * @time: 2016/04/11 6 */ 7 public class NimGame { 8 public boolean canWinNim(int n) { 9 return n % 4 != 0; 10 } 11 public static void main(String[] args) { 12 System.out.println(new NimGame().canWinNim(7)); 13 } 14 }

浙公网安备 33010602011771号