Description:

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.

有一堆石头,两个人轮流取,一次只能取1~3个,取得最后一个石头的人赢。给一个整数n判断是不是能赢。

这个问题其实是一个著名的博弈论的问题,但是这个地方有点简单,列出来前12个答案就能找出规律,就是只要是4的倍数都不可能赢。

Nim Game:http://www.guokr.com/blog/777525/   http://www.cnblogs.com/exponent/articles/2141477.html

代码:

public class Solution {
public boolean canWinNim(int n) {
return !(n%==);
}
}

LeetCode——Nim Game的更多相关文章

  1. [LeetCode] Nim Game 尼姆游戏

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. LeetCode: Nim Game

    这题其实不太好想,用到了博弈论 1,2,3 能赢 4 输 5,6,7 赢 8 输 9,10,11 赢 12 输 那么结论就是4的倍数就是输,其他情况就能赢. 为什么会是这样呢?很好解释,根源就在4会输 ...

  3. LeetCode Nim Game (简单nim)

    题意: 有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家.你先取,问最后谁赢? 思路: n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数, ...

  4. 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)(转)

    最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...

  5. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. 【一天一道LeetCode】#292. Nim Game

    一天一道LeetCode 从今天开始,调整规律,不按顺序做,从easy开始! 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 ...

  7. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  8. 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)

    最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...

  9. 【LeetCode】292. Nim Game 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    严重:   Error   configuring   application   listener   of   class   org.springframework.web.context.Co ...

  2. Kubeadm 安装

    version 1.7.0 已解决: echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables error: faile ...

  3. C#页面前台<%%><%#%><%=%>

    ASP.net前台绑定用的最多,今天小小总结一下. 1:<%#Eval("")%> 2:<%#Bind("")%> 3:<%=变量 ...

  4. C# IP地址与数字之间的互转

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Te ...

  5. 深入浅出:全面理解SQL Server权限体系

    转自IT168  好文转载存档! [IT168 技术]权限两个字,一个权力,一个限制.在软件领域通俗的解释就是哪些人可以对哪些资源做哪些操作.在SQL Server中,"哪些人", ...

  6. [Learn AF3]第一章 如何使用App Framework 3.0 构造应用程序

    af3的变化非常大.参见[译]Intel App Framework 3.0的变化 一.应用需要引用的js脚本: af3中不在自己实现dom选择器,而是选择基于jquey或兼容jquery的库如zep ...

  7. UNIX环境编程学习笔记(16)——进程管理之进程环境变量

    lienhua342014-10-03 1 环境表和环境指针 在每个进程启动时,都会接到一张环境表.环境表是一个字符指针数组,其中每个指针包含一个以 null 结束的 C 字符串的地址.全局变量env ...

  8. 和我一起学《HTTP权威指南》——Web服务器

    Web服务器 Web服务器会做些什么 1.建立连接(接受或关闭一个客户端连接) 2.接收请求(读取HTTP报文) 3.处理请求(解释请求报文并采取行动) 4.访问资源 5.构建响应(创建带有正确首部的 ...

  9. JS组件系列——表格组件神器:bootstrap table 包含了js对象的定义和对象成员函数的定义

    前言:之前一直在忙着各种什么效果,殊不知最基础的Bootstrap Table用法都没有涉及,罪过,罪过.今天补起来吧.上午博主由零开始自己从头到尾使用了一遍Bootstrap Table ,遇到不少 ...

  10. 03-Linux各目录及每个目录的详细介绍

    Linux各目录及每个目录的详细介绍 [常见目录说明] 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所 ...