- 浏览: 174710 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
拿石子的游戏。假设有一堆石子,没人每次可以拿1个,两个,最多三个。如果是你先拿,让你判断你是否会赢。假设玩游戏的人都很聪明,知道怎样回赢。题目中的提示假设石子有四颗,无论我们拿几颗我们都会输,对方肯定能一次拿走剩余的石子。如果是八颗石子也是一样的。事实上石子的个数为4的倍数我们肯定就会输。代码很简单,判断是否为4的倍数,代码如下:
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.
拿石子的游戏。假设有一堆石子,没人每次可以拿1个,两个,最多三个。如果是你先拿,让你判断你是否会赢。假设玩游戏的人都很聪明,知道怎样回赢。题目中的提示假设石子有四颗,无论我们拿几颗我们都会输,对方肯定能一次拿走剩余的石子。如果是八颗石子也是一样的。事实上石子的个数为4的倍数我们肯定就会输。代码很简单,判断是否为4的倍数,代码如下:
public class Solution { public boolean canWinNim(int n) { return n % 4 == 0 ? false : true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 232You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 348Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 342Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 461Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 525Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 435Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 624Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 435The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 542Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 379All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 870Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 885Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 560Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 611Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
matlab开发-NIMgame。NIM游戏的图形用户界面实现
Nim游戏(The game of Nim)是一个很著名而且有很多版本的游戏.下面这个版本有一个有趣的获胜策略.两名参与者交替从一堆石子中取出若干数目,其个数由参与者自已决定.但是要求参与者每次至少取出一个,至多取出一半,然后...
nim小游戏 可在dos系统下与计算机玩游戏 让计算机和人较量.在10到100之间生成一个随机数作为初始的石子数目.然后,随机产生0或1,以决定是计算机先玩还是人先玩.然后,再随机产生0或1,以决定计算机采用"聪明"还是"愚蠢...
Nim game introduction
尼姆游戏 尼姆游戏 IA 播放器的传统游戏。 规则:对于两个玩家:user 和 Iplayer 棋盘上有一定数量的棋子(早期版本只有 5) 第一个动作可以选择任意数量的棋子,但最小为 1,最大为棋子数减 1。...
nimgame:用于Nimrod语言的简单2D游戏引擎
nimgame2:用于Nim语言的简单2D游戏引擎
NimGame 尼姆游戏使用说明您将与Nim博士对战! 将会产生大量的弹珠,您的任务是拿走弹珠,直到一个玩家拿走最后一个弹珠为止。 最后一个弹珠的人输了! 您最多只能占用每回合剩余弹珠总数的一半。 举例:如果还有40...
ng2planetoids:用于nimgame2引擎的演示游戏
尼姆的古老而光荣的游戏 Nim是一款具有多种变体的古老游戏。 这是一个:两名玩家轮流从一堆中取出大理石。 在每一回合中,玩家必须移走至少一个但不超过剩余弹珠的一半。 被迫清除最后一块大理石的牌手输了。
Swift:以Nim编写的游戏引擎,针对快速制作出色的游戏进行了优化
玩家和电脑轮流取出筹码。 每个人都可以根据需要从一堆筹码中取出尽可能多的筹码。 赢得在棋盘上获得最后筹码的玩家。 反向玩,最后一个筹码者输了。 单击一个筹码会删除它以及它上面的筹码。 作弊按钮为您做出最好...
Reinforcement_NIM_Game:NIM游戏中的增强学习
Game-of-Nim
game of nim 21 -javascript 版本
Nim Game 模拟 Reverse Integer Palindrome Number Insert Interval Merge Intervals Minimum Window Substring Multiply Strings Substring with Concatenation of All Words Pascal's Triangle Pascal's Triangle ...
Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player’s move consists of removing one or more stones from any single pile. Play ends when ...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
The game of nim is played as follows. Some number of sticks are placed in a pile. Two players alternate in removing either one or two from the pile. The player who remove the last stick is the loser....
(./solutions/NimGame.cc) | 第258话| [AddDigits.cc] (./solutions/AddDigits.cc) | 104 | | [MaxDepthBinaryTree.cc] (./solutions/MaxDepthBinaryTree.cc) | 第226话| [InvertBinaryTree.cc] (./solutions/Invert...