题目

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:

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?

分析

这是博弈论中极为经典的尼姆游戏。有总数为n的石头,每个人可以拿1~m个石头,两个人交替拿,拿到最后一个的人获胜。究竟是先手有利,还是后手有利?

1个石子,先手全部拿走;

2个石子,先手全部拿走;

3个石子,先手全部拿走;

4个石子,后手面对的是先手的第1,2,3情况,后手必胜;

5个石子,先手拿走1个让后手面对第4种情况,后手必败;

6个石子,先手拿走2个让后手面对第4种情况,后手必败;

……

容易看出来,只有当出现了4的倍数,先手无可奈何,其余情况先手都可以获胜。 (石子数量为4的倍数)

后手的获胜策略十分简单,每次取石子的数量,与上一次先手取石子的数量和为4即可; (石子数量不为4的倍数)先手的获胜策略也十分简单,每次都令取之后剩余的石子数量为4的倍数(4*0=0,直接拿光),他就处于后手的位置上,利用上一行的策略获胜。

AC代码

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

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

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  4. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. HTML——传统布局的使用

    传统布局:使用table来做整体页面的布局 总结:这种方式来制作页面现在也不是很多了,感觉并不是很高效. 需要先用photoshop量出页面布局具体的尺寸位置,再将其划分为表格,对每个格子进行编辑. ...

  2. Linux上常用命令整理(二)—— paste

    上一篇整理了cat指令的几个基本常见用法,这次整理一下paste指令的基本用法. cat paste cut grep paste paste可以简单的理解为把两个文件的内容按列合并,与cat命令直接 ...

  3. Django 的一些错误以及处理

    django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 589: 'static', expected 'e ...

  4. cmd下一些常用的命令

    systeminfo   列出主机名,内存等一些系统信息 tasklist 显示进程信息      (tasklist | finstr eclipse)

  5. node.js安装Oracledb指导文档

    https://community.oracle.com/docs/DOC-931127

  6. 通过sqlserver sa密码修改windows操作系统密码

    如果你不记得windows管理员的密码了,但知道sqlserver sa用户的密码,可以通过以下方式修改: 进入SQL之后执行以下语句: -- 允许配置高级选项  EXEC sp_configure ...

  7. Mysql一个表编码的坑,mark一下

    问题:一个sql执行很慢,5分钟左右,关键是最大的表是5万出头,另一张表不到5000原因:是两个表的字符集不同,导致匹配时,没有匹配到 解决办法:将两个表的字符集改成一样具体的命令: ALTER TA ...

  8. 关于下载文件封装的两个类(Mars)

    首先是文件FileUtils.java package mars.utils; import java.io.File; import java.io.FileOutputStream; import ...

  9. I/O操作总结(一)

    所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后) 对于文件内容的操作 ...

  10. html学习笔记-XML-Javascript

    html学习笔记-XML-Javascript Table of Contents 1. XML HTTP Request 1.1. XMLHttpRequest 对象 1.2. 创建 XMLHttp ...