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.

Hint:

  1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?

思路:

当heap中有1、2、3个stones时一定获胜,当有4个stones时一定会输。那么当有5个stones的时候,我们可以先拿出来一个stones,此时相当于我们面对4个stones时的情形。

依次可以类推出,当heap中恰好有4N(N=1,2,3......)个stones时,我方一定会输。

java代码:

public class Solution {
public boolean canWinNim(int n) {
if(n%4==0) return false;
else return true;
}
}

LeetCode (262):Nim Game的更多相关文章

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

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

  2. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  3. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  4. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

  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 解题报告

    题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  7. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  8. [LeetCode] 292. Nim Game_Easy tag: Math

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

  9. LeetCode: 292 Nim Game(easy)

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

随机推荐

  1. 【转载】Java并发编程:volatile关键字解析

    http://www.cnblogs.com/dolphin0520/p/3920373.html

  2. java面试基础题------》int Integer Integer.valueOf

    在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...

  3. HDU_5514_Frogs

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. Restful and 前后端分离---AutoTest newman--postman

    http://www.cnblogs.com/zuoshaowei/p/6192863.html https://www.getpostman.com/docs/newman_intro swagge ...

  5. Redis的基本使用(基于maven和spring)

    使用redis基本测试 maven导包 <dependency> <groupId>redis.clients</groupId> <artifactId&g ...

  6. Cyclic Nacklace ---hdu3746(循环节,kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. abca---添加bc ...

  7. python argparse命令行参数

    测试: 第一个没有任何输出和出错 第二个测试为打印帮助信息,argparse会自动生成帮助文档 第三个测试为未定义的-v参数,会出错 第四个测试为未定义的参数foo,出错 positional arg ...

  8. C#建WindowForm调用R可视化

    众所周知R软件功能非常强大,可以很好的进行各类统计,并能输出图形.下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上的方法,文中介绍的很详细,需要的朋友可以参考下. ...

  9. [golang note] 匿名组合

    匿名组合 golang也提供了继承机制,但采用组合的文法,因此称为匿名组合.与其他语言不同, golang很清晰地展示出类的内存布局是怎样的. • 非指针方式组合 ▶  基本语法 // 基类 type ...

  10. gdb core

    程序运行发生异常退出,比如segment错误,此时可以利用系统生成的core文件,配合GDB来定位问题. 问题程序: segment.c #include <stdio.h> #inclu ...