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.

题目:

你和你的好基友在玩一个叫Nim的游戏:桌子上有一堆小石子,你或你基友轮流从中拿出1到3颗小石子。能拿完最后剩下的所有小石子的人获胜。你先开始拿。

假设:你和你基友都是IQ非常高的人,而且都是玩这个游戏的老手(深谙玩这个游戏的套路)。

你能否写个函数,用来计算对给定数目的小石子,你是否能赢下比赛。

eg:如果有4个小石子,你肯定会输的,因为不管你拿走1个、2个还是3个,最后剩下的小石子都会被你好基友拿完。

分析:如果轮到你拿的时候,剩下4个小石子,你就悲剧了。

    同理,如果你拿的时候还剩下8个小石子,不管你选择拿1个、2个还是3个,你朋友都能让你下次拿的时候剩下4个。

    递推地:假设在轮到你你拿的时候还剩4*n个小石子,不管你选择拿1个、2个还是3个,你朋友都能让你下次拿的时候还剩4*(n-1)个;

    也就是,你朋友可以让你每次拿之前,都剩4n个小石子;(n为大于等于1的正整数);这样递推下去,最后你就输了。

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

  

【算法功底】LeetCode 292 Nim Game的更多相关文章

  1. 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 ...

  2. 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个石子之后都是必胜,则当前必败 ...

  3. Java实现 LeetCode 292 Nim游戏

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

  4. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  5. LeetCode 292. Nim Game

    Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...

  6. Java [Leetcode 292]Nim Game

    问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...

  7. LeetCode 292 Nim Game 解题报告

    题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  8. LeetCode 292 Nim Game(Nim游戏)

    翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...

  9. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

随机推荐

  1. 在Angular中,如果权限值是异步请求所得,如何将其设置为HTTP请求头的Authorization?

    遇到此问题的背景:项目需要实现单点登录,在前后端分离的前提下,前台如何保存token值成为了一个问题.想到的解决方案是,将token值统一存到一个前端程序,其他的前端程序去这个前端程序去取token( ...

  2. css伪元素用法大全

    本文主要讲解css伪元素的用法,有需要的朋友可以阅读此文.本文讲解的伪元素有:before,after. 什么是伪元素(Pseudo element)? 伪元素不是真正的元素,不存在与文档之中,所以j ...

  3. 走进javascript——解开switch之谜

    很早以前就觉得switch很怪异,或者说一直没太理解它,它怪异就怪异在非要给每个语句加上break;不然后面的语句就算不符合条件还是会执行,比如以下这段代码 var num = 2; switch(n ...

  4. 智能打印SDK---官方博客

    (开源准备:开源为百度云打包开源,GIT太慢,SVN没有免费的,TFS有免费的也慢的要命) 开源地址:http://pan.baidu.com/s/1miobLoO 电子商务越来越成熟,提供的服务越来 ...

  5. 作为前端工程师,必须要学会的基本工具之一(AI)

    由于之前工作中一直使用的都是photoshop,所以对AI软件的使用并不是很熟,导致使用过程中,遇到了困难----字体无法选中.作为前端工程师都知道,一张设计图中有很多文字,如果纯手打那简直是要累死人 ...

  6. rem与em

    最近有朋友在进行rem布局的时候总搞不懂rem  em  px  与百分比布局的区别在哪里  这里简单给大家介绍一下 Em为单位: 这种技术需要一个参考点,一般都是以<body>的&quo ...

  7. 一个chrome图片下载插件的开发过程

    最近在做采集,发现用chrome的插件来下载整站也是一个不错的思路,所以想开发一个下载网页(仿站)的插件,学习过程如下: 首先查看一些文档资料 学习360翻译的开发文档:http://open.se. ...

  8. Express4.x动态的销毁或者替换中间件(app.unuse)

    需求描述 expres4.x托管静态资源时以中间件的方式将server-static挂载到app上,正常的使用没有问题,但是有时候我们需要动态的托管一些静态资源,也就是静态资源的目录不确定的时候该怎么 ...

  9. jquery 的基础知识,以及和Javascript的区别

    想到之前所学的javascript 我们会想到这几个方面:找元素: 操作内容: 操作属性:操作样式:统一操作元素: jquery 也是从这几个方面来学习的. <head> <meta ...

  10. Min Stack leetcode

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...