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.
Example:
Input:4
Output: false
Explanation: 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.
要完成的函数:
bool canWinNim(int n)
说明:
1、假设有一堆石头,你或者你的朋友每次能拿走1个或2个或3个石头,谁拿走了最后一个石头谁就赢。你是第一个拿的,你和你的朋友都非常聪明,每次做出的都是最优决策。现在给一个整数,表示石头的数量,返回你能不能赢。比如n=4,那么你无论如何都不能赢,因为无论你是拿了1个石头还是2个石头还是3个石头,你的朋友都能拿走最后的1个石头。
2、这道题笔者最开始以为又是一道动态规划的题目,但尝试了几个数之后,发现了规律。
n=1时,先出手的赢,所以你赢了。
n=2时,先出手的赢,所以你赢了
n=3时,先出手的赢,所以你赢了
n=4时,对方赢了,你输了。先出手的输了。
n=5时,你先出手,拿了1个石头,然后对方进入n=4这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。
n=6时,你先出手,拿了2个石头,然后对方进入n=4这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。
n=7时,你先出手,拿了3个石头,然后对方进入n=4这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。
n=8时,你先出手,无论你是拿了1/2/3个石头,对方都会进入n=7/6/5的状态,而且轮到对方出手了,他先出手就他赢了。你输了,先出手的输了。
n=9时,你先出手,拿了1个石头,然后对方进入n=8这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。
n=10时,你先出手,拿了2个石头,然后对方进入n=8这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。
n=11时,你先出手,拿了3个石头,然后对方进入n=8这种状态,而且轮到对方出手了,所以他先出手就输了。所以你赢了,先出手的赢了。
n=12时,你先出手,无论你是拿了1/2/3个石头,对方都会进入n=11/10/9的状态,而且轮到对方出手了,他先出手就他赢了。你输了,先出手的输了。
……
不知大家观察出来没有,只要n不是4的整数倍,你都可以控制对方进入n是4的整数倍,比如n=4或者n=8这种状态,然后对方先出手就对方输了。
而如果n是4的整数倍,那么就轮到对方控制你进入n是4的整数倍的状态,比如n=12,然后无论你怎么拿,对方都可以控制你进入n=8的状态,然后无论你再怎么拿,对方又控制你进入n=4的状态,然后n=4时,谁出手谁输。
所以代码很简单,如下:
bool canWinNim(int n)
{
if(n%4==0)
return false;
return true;
}
上述代码实测2ms,beats 100.00% of cpp submissions。
leetcode-292-Nim Game(搬石子)的更多相关文章
- 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个石子之后都是必胜,则当前必败 ...
 - 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 ...
 - Java实现 LeetCode 292 Nim游戏
		
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
 - 【算法功底】LeetCode 292 Nim Game
		
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
 - LeetCode 292. Nim Game (取物游戏)
		
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
 - LeetCode 292. Nim Game
		
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
 - Java [Leetcode 292]Nim Game
		
问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...
 - LeetCode 292 Nim Game 解题报告
		
题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...
 - LeetCode 292 Nim Game(Nim游戏)
		
翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确 ...
 - [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 ...
 
随机推荐
- jquery中的属性和样式设置
			
添加属性 $target.attr({"title":"one piece","name":"solgan"}); 为目 ...
 - 如何规范移动应用交互设计?UI/UX设计师须知的11个小技巧
			
以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 十年前,手机的使用只是为了沟通. 而近几年,情况发生了很大变化,我们很难找到不使用手机的人.手机在极 ...
 - ToList和ToDataTable(其中也有反射的知识)
			
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Refle ...
 - MDK5使用技巧
			
1.文本美化 修改 修改字体以及颜色如下: 修改用户自定义的关键字,如下: 代码编辑技巧 1.TAB键的妙用 使用TAB键可以整体向右移动相应位,使用SHIFT+TAB键整体左移相应位. 2.快速定位 ...
 - cannot be cast to
			
java.lang.ClassCastException: com.service.impl.OrderPlanServiceImpl cannot be cast to com.provider.s ...
 - Git config 配置文件
			
一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一次:即使你升级了,他们也会绑定到你的环境中.你也可以在任何时刻通过运行命令来重新更改这些设置. Git有一个工具 ...
 - An error "Host key verification failed" when you connect to other computer by OSX SSH
			
Here's quick way to remove all entries in the host file: In an OSX terminal, type rm -f ~/.ssh/known ...
 - Android-FileUtils-工具类
			
FileUtils-工具类 是对文件操作处理的方法进行了封装,提供了公共的方法: package common.library.utils; import android.annotation.Sup ...
 - CSS Animation triggers text rendering change in Safari
			
薄荷新首页上周五内测,花哥反馈在 MacBook Safari 浏览器下 鼠标移动到第一个商品的时候后面几个商品的文字会加粗.这是什么鬼??? 待我回到家打开笔记本,鼠标蹭蹭蹭的发现问题远不止如此: ...
 - postgres数据库参数配置说明介绍
			
访问 1. listen_addresses 监听访问地址 2. port 监听端口 3. max_connections 最大连接数 4. 性能 1. shared_buffers PostgreS ...