翻译

你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头。每次你从中去掉1-3个。谁消除掉最后一个石头即为赢家。你在取出石头的第一轮。

你们中的每个人都有着聪明的头脑和绝佳的策略。写一个函数来确定对于给定的数字是否你能够赢得这场比赛。

比如,假设堆中有4个石头,那么你永远也无法赢得比赛:不管你移除了1、2或3个石头,最后一个石头都会被你的朋友所移除。

原文

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.

分析

题目一開始不是非常理解,由于朋友假设有2个、3个、多个的情况是全然不一样的呐,后来细致一看原文第一句withyourfriend,发现仅仅是和1个朋友玩游戏。

于是我设定了一个推断条件

bool yourTrun = true;

后面巴拉巴拉写了一堆代码全错……

加上一天的劳苦我開始趴着睡觉了,脑子里还在回忆。忽然发现:

1-true
2-true
3-true
4-false
5-true
6-true
7-true
8-false

然后抬起头又一次写了一遍:

bool canWinNim(int n) {
if ((n - 1) % 4 == 0 || (n - 2) % 4 == 0 || (n - 3) % 4== 0) return true;
else return false;
}

哇。通过了,感觉整理整理:

bool canWinNim(int n) {
return (n - 1) % 4 == 0 || (n - 2) % 4 == 0 || (n - 3) % 4 == 0;
}

继续整理。原来这么简单呐:

bool canWinNim(int n) {
return n % 4 != 0;
}

忽然就不困了。

^_^

代码

class Solution {
public:
bool canWinNim(int n) {
return n % 4 != 0;
}
};

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

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

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

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

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

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

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

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

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

  5. LightOJ 1253 Misere NIM(反NIM博弈)

    Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, 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, eac ...

  7. Java实现 LeetCode 292 Nim游戏

    292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...

  8. leetcode 292. Nim游戏(python)

    你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断 ...

  9. 292 Nim Game Nim游戏

    您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 到 3 块石头. 拿掉最后一块石头的人就是胜利者.由您来开局.你们两个都是聪明人,相信都有最佳的游戏策略. 请编写一个函 ...

随机推荐

  1. 任务调度框架FluentScheduler简介

    之前我在文章中介绍过.net中的任务调度框架Hangfire,HangFire虽然本身输入比较简单好用的,但是,如果我们的程序本身提供的服务不是任务调度,而任务调度只是里面并不重要的小功能的时候,用H ...

  2. Technical Information ARM-related JTAG / SWD / SWV / ETM Target Interfaces

    https://www.computex.co.jp/eg/products/pdf/technical_pdf/arm_if01_gijutsu_eng.pdf

  3. JVM菜鸟进阶高手之路

    http://www.jianshu.com/u/3def157aab07?utm_medium=note-author-link&utm_source=mobile

  4. 加快Qemu Aarch32虚拟开发板的启动速度

    软件版本 Qemu: 2.8.0 虚拟开发板: vexpress-ca9 概述 之前的博文介绍了将Python移植到开发板上, 根文件系统采用的是ramdisk, 这个文件系统的缺点是修改的内容重启会 ...

  5. ExtJS动态设置表头

    if(document.getElementById("lxdj_radio").checked){ colQd = new Ext.grid.ColumnModel(colMAr ...

  6. 设计模式之代理模式之二(Proxy)

    from://http://www.cnblogs.com/xwdreamer/archive/2012/05/23/2515306.html 设计模式之代理模式之二(Proxy)   0.前言 在前 ...

  7. 转: MySQL5.7 ERROR 1142 (42000)问题

    1,mysql全库导入报错 [root@dev_121_21 ~]# mysql--socket=/usr/local/mysql/mysql.sock --default-character-set ...

  8. 为python安装matplotlib模块

    matplotlib是python中强大的画图模块. 首先确保已经安装python,然后用pip来安装matplotlib模块. 进入到cmd窗口下,执行python -m pip install - ...

  9. ExtJS 4.2 教程-07:Ext.Direct

    转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-7-Ext-Direct ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 教程 ...

  10. 详细解读DiskLruCache

    DiskLruCache这个类我记忆中是来自Google的一个开源项目,叫做BitmapFun,目的是更方便的加载bitmap.项目的源码:https://developer.android.com/ ...