LeetCode Nim Game (简单nim)

题意:

  有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家。你先取,问最后谁赢?

思路:

  n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数,而对方最多只能取到3个,剩下的给先手来取,所以先手赢。

1 class Solution {
2 public:
3     bool canWinNim(int n) {
4         return n%4>0;
5     }
6 };

AC代码

时间: 2024-10-24 22:17:01

LeetCode Nim Game (简单nim)的相关文章

?HDU 5795 A Simple Nim(简单Nim)

HDU 5795 A Simple Nim(简单Nim) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On e

HDU 3032 Nim or not Nim?(博弈 SG打表找规律)

传送门 Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1457    Accepted Submission(s): 720 Problem Description Nim is a two-player mathematic game of strategy in which players take

hdu 3032 Nim or not Nim? 博弈论,,,网上搜的题解让我大开眼界,原来还可以这样A题

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1076    Accepted Submission(s): 532 Problem Description Nim is a two-player mathematic game of strategy in which players take tur

去掉有序数组中重复数字 原地 leetcode java (最简单的方法)

1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

HDU 3032 Nim or not Nim?(sg函数)

题目链接 暴力出来,竟然眼花了以为sg(i) = i啊....看表要认真啊!!! #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define LL __int64 int dp[10001]; int sg(int x) { int flag[10001],temp,i; if(dp[x] >= 0) return dp[x]; memset(flag,

【HDU3032】【Lasker&#39;s Nim(一种Nim游戏)】Nim or not Nim? Multi-SG博弈、打表

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42652745 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意:n堆石子,每次可以从某堆中拿走若干,也可以把此堆分成两个非空堆,谁无法操作了谁输. 题解:首先我们可以打个SG函数来暴力出解,但是显然这会T. 但是不要害怕,我们打完以后发现了一个貌似对的规律: 对于所有的k >= 0,有 sg( 4k+1 ) = 4k+1: sg( 4k+2 ) = 4k+2: sg(

hdu 3032 Nim or not Nim? sg函数

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1056    Accepted Submission(s): 523 Problem Description Nim is a two-player mathematic game of strategy in which players take turn

hdu3032 Nim or not Nim?(SG函数)

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1710    Accepted Submission(s): 845 Problem Description Nim is a two-player mathematic game of strategy in which players take tur

HDU 3032 Nim or not Nim? (sg函数求解)

Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provide

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in