292. Nim Game(C++)
292. Nim Game(C++)
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?
题目大意:
解题方法:
这题往小说可以追溯到小学奥数或者脑筋急转弯的书中,往大说可以深究到博弈论。然而编程在这里并没有卵用,策略在于,因为每个人都取不到4个,假设自己后 走,要保证每轮自己和对方取得数量的和是4,这样就能确保每轮完后都有4的倍数个石头被取走。这样,如果我们先走的话,先把n除4的余数个石头拿走,这样 不管怎样,到最后都会留4个下来,对方取1个你就取3个,对方取2个你就取2个,就必赢了。
注意事项:
C++代码:
class Solution
{
public:
bool canWinNim(int n)
{
return n%!=;
}
};
292. Nim Game(C++)的更多相关文章
- 292. Nim Game
292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on ...
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- 【Leetcode】292. Nim Game
problem 292. Nim Game solution class Solution { public: bool canWinNim(int n) { ; } }; 来generalize一下 ...
- 292. Nim游戏
292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- 292. Nim Game - LeetCode
Question 292. Nim Game Solution 思路:试着列举一下,就能发现一个n只要不是4的倍数,就能赢. n 是否能赢 1 true 2 true 3 true 4 false 不 ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
随机推荐
- Server.Transfer方法,Server.Execute方法和Response.Redirect方法有什么异同
(1)Server.Transfer方法: Server.Transfer("m2.aspx");//页面转向(服务器上执行). 服务器停止解析本页,保存此页转向前的数据后,再使页 ...
- Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍
磁盘分区表.文件系统的查看.统计的工具很多,有些工具是多功能的,不仅仅是查看磁盘的分区表,而且也能进行磁盘分区的操作:但在本文,我们只讲磁盘分区的查看,以及分区的使用情况的查看:本文只是给新手上路之用 ...
- Python异常处理实例
#coding=utf-8 #---异常处理--- # 写一个自己定义的异常类 class MyInputException(Exception): def __init__(self, length ...
- unity3d Realistic eye shading 真实的眼睛渲染
先放上效果 人皮都做了,当然要来研究下眼睛,眼睛要比人体皮肤简单一些(实时模拟人皮在此) 一看是不是很复杂 这是眼睛的解构,但是,我们只需要模拟出虹膜巩膜和角膜就能达到相当真实的眼睛 巩膜就是白眼球 ...
- php 错误信息配置
display_errors = on/off错误回显,一般常用语开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴露出非常多的敏感信息,为攻击者下一步攻击提供便利.推荐关闭此选项. ...
- BZOJ4195 [Noi2015]程序自动分析(离散化+并查集)
4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 689 Solved: 296 [Submit][Sta ...
- Android开发工具GenyMotion安装和使用方法
好长时间没有再接触Android了,以至于GenyMotion出现这么久了,我还没有试用过,记得当时发布Android Studio时,当天我就开始试用了,好吧,看到GenyMotion这 ...
- 程序员取悦女朋友的正确姿势---Tips(iOS美容篇)
前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即可完成自定义滤镜渲染照片.app独一无二,虽简亦繁. JH定律:魔镜:最漂亮的女人是你老婆魔镜: ...
- hdu2817 A sequence of numbers
这题就是判断是等差数列还是等比数列,然后计算结果mod200907 因为数字比较大10的九次方 所以等比用到了快速幂求模 不懂可以看看算法导论,在大数那里有讲 #include <iostrea ...
- 用Excel完成专业化数据统计、分析工作
使用Excel可以完成很多专业软件才能完成的数据统计.分析工作,比如:直方图.相关系数.协方差.各种概率分布.抽样与动态模拟.总体均值判断,均值推断.线性.非线性回归.多元回归分析.时间序列等.本专题 ...