【Leetcode】Nim Game
2015-10-26 23:18
351 查看
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.
--------------------------------------------------------------------------
The solution is quite simple if you figure out the trick for playing this game. Focusing on the hint, we could know that 4 is a significant number. Why? It's because 4 is the least unit that you could pick up every round regardless of any number your enemy
post. If he post 3, you post 1; If he post 2, you post 2; If he post 1, you post 3. Thus, If the initial number is 13 and you will take the first move, you just need to take 1 for the first round. Then no matter what number he chose in any round, you just
choose (4-x) to complete until reach 13, which also means if the initial number is like 12 or 20, those could be divided by 4, you would definitely fail if your enemy is as clever as you.
I wrote this solution in JAVA for the record:
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.
--------------------------------------------------------------------------
The solution is quite simple if you figure out the trick for playing this game. Focusing on the hint, we could know that 4 is a significant number. Why? It's because 4 is the least unit that you could pick up every round regardless of any number your enemy
post. If he post 3, you post 1; If he post 2, you post 2; If he post 1, you post 3. Thus, If the initial number is 13 and you will take the first move, you just need to take 1 for the first round. Then no matter what number he chose in any round, you just
choose (4-x) to complete until reach 13, which also means if the initial number is like 12 or 20, those could be divided by 4, you would definitely fail if your enemy is as clever as you.
I wrote this solution in JAVA for the record:
package testAndfun; public class Nim { public static void main(String args[]){ int n1 = 13; int n2 = 20; Nim nim = new Nim(); System.out.println("It's "+nim.canWinNim(n1)+" to win the game"); System.out.println("It's "+nim.canWinNim(n2)+" to win the game"); } public boolean canWinNim(int n) { return n%4!=0; } }
相关文章推荐
- Ajax
- 几种ESB(企业服务总线)介绍
- Opencv 鼠标事件GUI
- [Leetcode]Path Sum II
- Windows 10还原为Windows 8/7
- 静态
- 【2015/10/24】C学习日志_Day 9 关键字
- 文章标题
- C++Primer笔记一
- jackson学习----解析豆瓣的图书信息
- 【黑马程序员】成为一匹黑色战马,驰骋于技术的荒野之中
- android群英传读书笔记---自定义ViewGroup
- Thinking in Java 整理笔记:内部类
- FZU - 1901 Period II(KMP next数组的理解)
- 系统系统项目管理工程师20151022作业
- LeetCode:Add Digits
- C++:流类库与输入输出
- iOS-ViewController的生命周期
- pull解析xml文件
- JAVA ,Map接口 ,迭代器Iterator