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:win

2: win

3: win

4: 无论如何都无法win

5:拿1,让对方面对4

6:拿2,让对方面对4

7:拿3,让对方面对4

8:没有办法

.....

可见,当石头数量是4的倍数的时候,先手是无可奈何的,所以代码就很简单了,如下:

 class Solution {
public:
bool canWinNim(int n) {
return n%;
}
};

LeetCode OJ:Nim Game(Nim游戏)的更多相关文章

  1. 51nod1069【Nim取石子游戏】

    具体看:萌新笔记之Nim取石子游戏可以这么写: #include <bits/stdc++.h> using namespace std; typedef long long LL; in ...

  2. 萌新笔记之Nim取石子游戏

    以下笔记摘自计算机丛书组合数学,机械工业出版社. Nim取石子游戏 Nim(来自德语Nimm!,意为拿取)取石子游戏. 前言: 哇咔咔,让我们来追寻娱乐数学的组合数学起源! 游戏内容: 有两个玩家面对 ...

  3. Light OJ 1253 Misere Nim (尼姆博弈(2))

    LightOJ1253 :Misere Nim 时间限制:1000MS    内存限制:32768KByte   64位IO格式:%lld & %llu 描述 Alice and Bob ar ...

  4. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  5. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  6. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  7. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  8. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  10. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

随机推荐

  1. Ubuntu16.04系统中Nmon的安装

    Nmon的安装,亲民的安装方法: apt install nmon 不再需要源码编译安装和下载文件到指定目录,解压再运行了.

  2. Python中正则模块re.compile、re.match及re.search函数用法

    import rehelp(re.compile)'''输出结果为:Help on function compile in module re: compile(pattern, flags=0) C ...

  3. oracle中自定义type、以及java中传递list到过程中的例子

    在java开发过程中有时候为了处理数据的速度更快,会把要处理的数据组装成list,把list作为过程的一个参数,在过程中批量处理,下面就以一个例子做简单的阐述,以此谨记. --药品目录智能审核 --说 ...

  4. MVC readioButtonList的创作过程及运用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Li ...

  5. python文件打开的几种访问模式

    文件打开的几种访问模式 访问模式 说明 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. w 打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. a ...

  6. Spring Boot JPA 使用 Sql Service 与 不使用默认驼峰策略

    引入 数据源 <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>s ...

  7. LA 3971 组装电脑(二分)

    https://vjudge.net/problem/UVALive-3971 题意:你有b块钱,想要组装一台电脑.给出n个配件各自的种类.品质因子和价格,要求每种类型的配件各买一个,总价格不超过b, ...

  8. Decode Ways,编码方式数量求解。动态规划问题。

    问题描述: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  9. Codeforces gym 100971 D. Laying Cables 单调栈

    D. Laying Cables time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. RabbitMQ 的路由模式 Topic模式

    模型 生产者 package cn.wh; import java.io.IOException; import java.util.concurrent.TimeoutException; impo ...