Java [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.
解题思路:
这道题比较简单,就是判断给定的n是否为4的倍数。
如果n为4的倍数,那么作为先手,是没有机会获胜的,因为每次我走完,那么对方只要走的数目跟我这次相加为4即可。
如果n不为4的倍数,那么我第一次拿走的数目使得剩余的数目为4的倍数,这样就变成了前一种情况,只不过这次轮到我是后来走的人。
代码如下:
public class Solution {
public boolean canWinNim(int n) {
if (n % 4 == 0)
return false;
else
return true;
}
}
Java [Leetcode 292]Nim Game的更多相关文章
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- 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 ...
- 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个石子之后都是必胜,则当前必败 ...
- 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 ...
- 【算法功底】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 ...
- 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 ...
随机推荐
- 关于python多线程编程中join()和setDaemon()的一点儿探究
关于python多线程编程中join()和setDaemon()的用法,这两天我看网上的资料看得头晕脑涨也没看懂,干脆就做一个实验来看看吧. 首先是编写实验的基础代码,创建一个名为MyThread的 ...
- 【转】#ifdef __cplusplus深度剖析
原文:http://bbs.ednchina.com/BLOG_ARTICLE_251752.HTM 时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern ...
- Linux 启动直接进入 console,
我的电脑上有两块显卡,上次fedora更新后,自动安装了nvidia的驱动, 然后悲剧发生了.再也不能够正常进行图形界面了.所以需要进入CONSOLE. 1. 当系统启动时,停止grub menu自动 ...
- EXTJS 3.0 资料 控件之 GridPanel属性与方法大全
1.Ext.grid.GridPanel 主要配置项: store:表格的数据集 columns:表格列模式的配置数组,可自动创建ColumnModel列模式 autoExpandColumn:自动充 ...
- POJ 3701 概率DP
给定2^n 支足球队进行比赛,n<=7. 队伍两两之间有一个获胜的概率,求每一个队伍赢得最后比赛的概率是多少? 状态其实都是很显然的,一开始觉得这个问题很难啊,不会.dp[i][j] 表示第i支 ...
- filter_map
#!/usr/bin/env python # -*- coding:utf-8 -*- ret = filter( lambda x: x < 3, [1, 2, 3, 4, 5]) prin ...
- 1064: [Noi2008]假面舞会 - BZOJ
Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办 ...
- 2326: [HNOI2011]数学作业 - BZOJ
首先是DP,分段DP(按位数讨论) 然后每一段构造出它对应的矩阵,用矩阵快速幂加速 type matrix=..,..]of int64; var n,m:int64; a,b,c,d:matrix; ...
- static 内部类
一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...
- 10 things you should know about NoSQL databases
For a quarter of a century, the relational database (RDBMS) has been the dominant model for database ...