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?
public class Solution {
public boolean canWinNim(int n) {
if(n%4 == 0)
{
return false;
}
else
{
return true;
}
}
}
/*************************************************************************
> File Name: LeetCode292.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 10 May 2016 07:30:06 PM CST
************************************************************************/ /************************************************************************* 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? ************************************************************************/ #include <stdio.h> int canWinNim( int n )
{
int ret = ;
if( n% != )
{
ret = ;
}
return ret;
} int main()
{
int n = ;
int ret = canWinNim(n);
printf("%d\n", ret);
return ;
}
LeetCode 292的更多相关文章
- 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]292之尼姆游戏(Nim Game)(转)
最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...
- 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)
最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...
- 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 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- Leetcode 292 Nim Game 博弈论
class Solution {public: bool canWinNim(int n) { return n % 4 != 0; }};
- Java [Leetcode 292]Nim Game
问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...
随机推荐
- homework-07
终于等到了一次难度相对比较小的问题,只要读完提问题就好,但又不得不说自己真的很菜,且不说C++掌握的不好,连英文也比较差,导致读的非常吃力,不过我还是坚持读完了四篇英文文章,大致意思是可以了解的,但是 ...
- [iOS基础控件 - 6.10.5] UIApplication
A.概念 1.UIApplication对象是应用程序的象征,每个应用都有 2.单例 3.[UIApplication sharedApplication] 获取 4.iOS启动创建的第一个对象 5. ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- HDU2066一个人的旅行(dijkstra)
一开始拿到这个题感觉floyd可能会超,还是写了写,果然1WA+1TLE,之后觉得用dijkstra试试看看S和D会不会比较小,还是1WA+1TLE,最后还是借鉴了别人的做法. 把他的家作为起点,与他 ...
- JAVA 反射应用:properties2Object
MixAll.java import java.lang.reflect.Method; import java.util.Properties; public class MixAll { /** ...
- Android MuPDF 部署
MuPDF是一款轻量级的开源软件,可以用来阅读PDF文件.下载完源代码以后,想要运行成功,除了Android SDK之外,还需要Android NDK环境,因此有点麻烦. 但是一旦安装完必须的环境以后 ...
- 用BenchmarkDotNet给C#程序做性能测试
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用BenchmarkDotNet给C#程序做性能测试.
- Apache POI 合并单元格
合并单元格所使用的方法: sheet.addMergedRegion( CellRangeAddress cellRangeAddress ); CellRangeAddress 对象的构造 ...
- SRV记录说明
SRV记录是DNS服务器的数据库中支持的一种资源记录的类型,它记录了哪台计算机提供了哪个服务这么一个简单的信息 SRV 记录:一般是为Microsoft的活动目录设置时的应用.DNS可以独立于活动 ...
- 部署WEB应用程序
部署WEB应用程序: 1.在模板机上新建IIS站点 2.安装WebDeploy后在IIS控制台中导出站点为应用程序包 其站点在新虚机上必须存在,否则会报错,如下: 应用程序(C:\ProgramDat ...