题目

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. net core WebApi 使用Swagger

    Asp.net core WebApi 使用Swagger生成帮助页 最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core ...

  2. angular4和asp.net core 2 web api

    angular4和asp.net core 2 web api 这是一篇学习笔记. angular 5 正式版都快出了, 不过主要是性能升级. 我认为angular 4还是很适合企业的, 就像.net ...

  3. (转)关于inode和block的两道企业面试题

    关于inode和block的两道企业面试题 原文:http://www.tk4479.net/xiaolong361/article/details/52373374 一.一个100M的磁盘分区,分别 ...

  4. windows下指定端口号起步memcached

    双击.exe启动的话,默认启动的端口是11211 ,要指定端口的话加 -p + 端口号,如: E:\tools\memcached-1.4.5-win32>memcached-1.4.5.exe ...

  5. HangFire 定时任务

    https://www.cnblogs.com/ecin/p/6201262.html#%E5%9F%BA%E4%BA%8E%E9%98%9F%E5%88%97%E7%9A%84%E4%BB%BB%E ...

  6. C#入门笔记3 表达式及运算符2

    关系运算符,也称布尔比较运算符 注:var1为bool类型,var2与var3可以是其它类型.[数据类型看下一节] 运算符 类别 示例表达式 结果说明 == 二元 var1=var2==var3 如果 ...

  7. java容器集合

    java 集合分为 Collection 和 Map 两大类 Collection 是 Java 集合框架的顶层接口,它是对容器类进行增.删.改.查的定义,同时继承了 Iterable 接口,具有对集 ...

  8. 【持续更新】Java 时间相关

    直接上代码: import java.util.*; import java.text.SimpleDateFormat; public class HelloWorld { public stati ...

  9. Mysql order by 排序 varchar 类型数据

    Mysql order by 排序 varchar 类型数据 varchar 类型字段排序,  会將数字当成字符串来处理.  排序规则一般是从左到右一位位来比较. +0之后 就转化成INT 类型排序 ...

  10. ios uilabel 根据文字 计算宽度 高度

    //根据宽度求高度        + (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFl ...