Nim Game
2015-12-22 11:13
288 查看
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.
public class Solution {
public boolean canWinNim(int n) {
if(n%4==0)//对于逻辑运算符&&,||,!,操作元必须是boolean型数(true或false)
return false;
else
return true;
}
}
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.
public class Solution {
public boolean canWinNim(int n) {
if(n%4==0)//对于逻辑运算符&&,||,!,操作元必须是boolean型数(true或false)
return false;
else
return true;
}
}
相关文章推荐
- 正确配置jstl的maven依赖
- Volley框架的使用
- C++命名空间
- C++ #if #endif #define #ifdef #ifndef #if defined #if !defined详解
- C++使用CHttpFile实现Http请求
- Dictionary对象
- Java方法进行解压/生成有密码保护的压缩文件(zip格式)
- 树莓派上的mysql数据库中文乱码问题
- Google Volley使用之自定义
- 128,NSnumber的应用
- 30.Evaluate the following CREATE TABLE commands:
- 使用Win32/ATL建立窗口的过程
- jquery easyui 弹出消息框 (转载) jQuery EasyUI API 中文文档 - 消息框(Messager) http://www.cnblogs.com/hantianwei/archive/2012/03/19/2407113.html
- http://home.cnblogs.com/
- Metronic – 模板,基于 Bootstrap 的响应式后台+前台 管理模板
- MyEclipse-6.5注册码生成器源码
- leetcode-- H-Index I & II -- 重点常考
- surfaceview缓存问题
- nginx服务器安装及配置文件详解
- 一篇文章入门Python生态系统