LeetCode(292) Nim Game
题目
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的更多相关文章
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- docker mysql安装
Docker MySQL-Server 安装1.搜索docker search mysql# 一般会选择mysql-server 版本 2.拉取 docker pull mysql-server 3. ...
- 前端开发如何做好SEO优化的工作
前端开发工程师不仅需要要跟视觉设计师.交互式设计师配合,完美还原设计图稿,编写兼容各大浏览器.加载速度快.用户体验好的页面.现在还需要跟SEO人员配合,调整页面的代码结构和标签. 一些成熟的平台,在开 ...
- MySQL存储过程多条修改语句
DROP procedure Sel_Function_ActivityPastDueDELIMITER $$DROP procedure IF EXISTS`shouyi`.`Sel_Functio ...
- 数据库2_sqlHelper
封装一个受影响的行 public static int ExcuteNonQuery(string sqlText,params SqlParameter[] parameters) { using ...
- AJPFX关于JDK,JRE,JVM的区别与联系
很多朋友可能跟我一样,对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天我们来整理下三者的关系. JDK : Java Development ToolKit(Java开发工具包) ...
- Log4j输出格式log4j的PatternLayout参数含义
摘自:http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html 参数 说明 例子 %c 列出logger ...
- 1008 选数 2002年NOIP全国联赛普及组
1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description ...
- android :fragmentation使用中遇到的 NullPointerException
背景:fragmentation(单ACTIVITY+多个fragments)+brvah( recyclerView多级自定义菜单功能) 目的:实现 菜单栏的点击,fragment 显示相应的内 ...
- 获取元素Bytagname区别/for循环应用
一:两种获取元素方式的区别.1.var aLi = oUl.getElementsByTagName('li');TagName前面可以加其他东西,id就只能是document,2,Id是静态的,ta ...
- gitinore修改不生效
.gitignore只能忽略那些尚未被被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的.一个简单的解决方法就是先把本地缓存删除(改变成未track状态),然后 ...