292. Nim Game

Easy

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.

Example:

Input: 4
Output: false
Explanation: 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.
package leetcode.easy;

public class NimGame {
public boolean canWinNim(int n) {
return (n % 4 != 0);
} @org.junit.Test
public void test() {
System.out.println(canWinNim(4));
}
}

LeetCode_292. 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. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  3. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  4. LeetCode 292. Nim Game

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

  5. 【SRM】518 Nim

    题意 \(K(1 \le K \le 10^9)\)堆石子,每堆石子个数不超过\(L(2 \le 50000)\),问Nim游戏中先手必败局面的数量,答案对\(10^9+7\)取模. 分析 容易得到\ ...

  6. HDU 2509 Nim博弈变形

    1.HDU 2509  2.题意:n堆苹果,两个人轮流,每次从一堆中取连续的多个,至少取一个,最后取光者败. 3.总结:Nim博弈的变形,还是不知道怎么分析,,,,看了大牛的博客. 传送门 首先给出结 ...

  7. HDU 1907 Nim博弈变形

    1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形. ...

  8. Nim游戏

    目前有3堆石子,每堆石子个数也是任意的,双方轮流从中取出石子,规则如下:1)每一步应取走至少一枚石子:每一步只能从某一堆中取走部分或全部石子:2)如果谁不能取谁就失败. Bouton定理: 必败状态当 ...

  9. HDU 3032 Nim or not Nim (sg函数)

    加强版的NIM游戏,多了一个操作,可以将一堆石子分成两堆非空的. 数据范围太大,打出sg表后找规律. # include <cstdio> # include <cstring> ...

随机推荐

  1. strtol函数的用法——字符串转长整形

    /* strtol example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* strtol */ in ...

  2. memset()函数的使用

    1.概述 memset()函数,称为按字节赋值函数,使用时需要加头文件 #include<cstring>或者#include<string.h>.通常有两个用法: (1)用来 ...

  3. GBDT 算法

    GBDT (Gradient Boosting Decision Tree) 梯度提升迭代决策树.GBDT 也是 Boosting 算法的一种,但是和 AdaBoost 算法不同(AdaBoost 算 ...

  4. PHP 面试服务器优化和大数据

    服务器配置优化 系统参数调整 Linux 系统内核参数优化 vim /etc/sysctl.conf net.ipv4.ip_local_port_range = 1024 65535 # 用户端口范 ...

  5. C二维数组用指针地址遍历

    #include <stdio.h> #include <stdlib.h> int main(){ int a = 100; void *p = &a; printf ...

  6. kindle touch 5.1.2 update your kindle 灰色 解决办法

    要出差了,于是把抽屉里的老Kindle Touch拿出来想升个级,baidu说多看费电,果断卸了用原生. 但是原生里面升级选项“update your kindle”是灰色的,没法点,怎么办? 试了半 ...

  7. top,free,df,iostat,netstat

    服务器程序员除了编写功能之外,若想往上走需要在外网环境打磨很久,以下是行走江湖的制胜法宝. top(综合查看) free(查看内存占用) ps aux|sort -nk6 (查看内存占用具体使用物理内 ...

  8. 《挑战30天C++入门极限》C++面向对象编程入门:类(class)

        C++面向对象编程入门:类(class) 上两篇内容我们着重说了结构体相关知识的操作. 以后的内容我们将逐步完全以c++作为主体了,这也意味着我们的教程正式进入面向对象的编程了. 前面的教程我 ...

  9. 小程序的基本原生js使用

    1.点击事件 <a data-current="{{setting.current}}" bindtap="clickcurrent" style=&qu ...

  10. macOS 系统下载地址

    macOS Catalina macOS Mojave macOS High Sierra macOS Sierra 点击可以直接在MAC App Store下载,如果不能下载就多试几次.