nim game c问题 求教数学高手

求助数学高手哦,教教,问题较多较难,请有心理准备~~
本回答由提问者推荐
var sogou_ad_id=731547;
var sogou_ad_height=160;
var sogou_ad_width=690;[LeetCode]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.
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?
题目大意:
你正在和朋友玩下面的Nim游戏:桌子上有一堆石头,每一次你们轮流取1至3颗石头。最后一个取走石头的人就是赢家。第一轮由你先取。
你们俩都很聪明并且掌握玩游戏的最佳策略。编写函数,给定石头的个数,判断你是否可以赢得游戏。
例如,如果堆中有4颗石头,那么你一定不会赢得游戏:无论你第一轮取走1,2,还是3颗石头,下一轮你的朋友都可以取走余下的所有石头。
如果堆中有5颗石头,你可以找出确保自己能赢的取石子策略吗?
解题思路:
Nim游戏的解题关键是寻找&必胜态&。
根据题设条件:
当n&[1,3]时,先手必胜。
当n == 4时,无论先手第一轮如何选取,下一轮都会转化为n&[1,3]的情形,此时先手必负。
当n&[5,7]时,先手必胜,先手分别通过取走[1,3]颗石头,可将状态转化为n == 4时的情形,此时后手必负。
当n == 8时,无论先手第一轮如何选取,下一轮都会转化为n&[5,7]的情形,此时先手必负。
以此类推,可以得出结论:
当n % 4 != 0时,先手必胜;否则先手必负。
Python代码:
class Solution(object):
def canWinNim(self, n):
:type n: int
:rtype: bool
return n % 4 & 0
本文链接:请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。
如果您喜欢这篇博文,欢迎您捐赠书影博客:
没有类似日志
Pingbacks已关闭。
欢迎来到的博客
周一周二周三周四周五周六周日
2022232425【图文】Nim_Game_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
&&Nim游戏,算法作业,xor实现nim游戏
大小:316.00KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢292. Nim Game - 简书
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.
纯数学题,这种题目一般找规律用数学归纳法
stones = 1:
拿一个,必赢
stones = 2:
拿两个,必赢
stones = 3:
拿三个,必赢
stones = 4: 不管拿几个,必输
stones = 5:
拿一个,必赢
stones = 6: 拿两个,必赢
stones = 7:
拿三个,必赢
stones = 8: 不管拿几个,必输
...........
由此可见 当 stones 是 4 的倍数是必输
bool canWinNim(int n) {
return n % 4;
// 无需考虑负数的情况,这个应该是调用者自己负责
Why Stock Markets CrashThis page intentionally left blankWhy Stock Markets CrashCritical Events in ComplexFinancial SystemsD i d i e r S ...
翻译:漫步寰宇、纯素和平 Once again, this is the beginning of the 33rd Keshe Knowledge Seekers Workshop 第33次凯史知识寻求者课程 and we'll be tuning into the Sp...
PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE AGREEMENT TERMS AND CONDITIONS CAREFULLY BEFORE DOWNLOADING OR USING THE APPLE ...
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...
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 remov...
2.1 基本数据类型 2.1.1变量 1变量的取名 1.变量名的第一个字符必须是字母或者下划线 2.1.2变量类型 整型 int与long long 何时使用 对于整型的分为int和long long 但是初学者经常分不清何时使用,这里给个具体的数据: 在绝对值在10^9的...
说到职业病,那么最先知道的便是尘肺病,那么你知道尘肺病这种职业病,是哪些职业带来的么、该如何面对这种病呢? 尘肺病: 尘肺病是一种肺部纤维化疾病,是我国职业病防治法中规定的职业病类别的第一大类,它是由于职业性接触生产性粉尘引起的,尘肺病的发生和粉尘接触量有明确的依赖关系,即...
前两天主管学生留学交换的老师给我发了一封邮件,说已经收到了首尔大学寄给学校的成绩单。除此之外她还特别说了一句, “congratulations, you did really well”。但是我却没有任何兴趣或者是渴望去看,或者去联系首尔大学教务的老师,看看我到底是拿了1...
冬至,雾霾天,整个城市灰蒙蒙的,嘉陵江面与桥身朦朦胧胧,遗憾有一点脏脏的味道,穿越城市隧道,跨过左右无崖的立交桥,转而进入拥挤的小巷,远处飘来的烟,很清晰地能辨别不是霾是羊肉浓汤,香得那个喷喷,店堂里很拥挤,一个桌可能拼了三拨人,油辣泼香的红烧羊肉,奶白鲜香的羊毛肚汤,桌邻...Nim游戏Nim游戏定义Nim游戏是组合游戏(Combinatorial Games)的一种,准确来说,属于“Impartial Combinatorial Games”(以下简称ICG)。满足以下条...
博弈论应该算是一门独立的学问吧,它是现代数学不断进步的产物,是运筹学中重要的一部分。作为一个计算机科学与技术专业的学生,在这里谈论这高深的“博弈”二字实有不妥,所以,讲的不好的地方请多见谅。
博弈论(一):Nim游戏
重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算。
若你想仔细学习博弈论,我强烈推荐加利福尼亚大学的Thomas S. Ferguson教授精心撰写并免费提供的这份教材,它使我受益太多。(如果你的英文水平不足以阅读它,我只能说,恐怕你还没到...
重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P-position当且仅当a1^a2^...^an=0,其中^表示位异或(xor)运算。
Nim游戏是博弈论中最经典的模...
题目来自于hihocoder':http://hihocoder.com/problemset/problem/1163?sid=423214
时间限制:10000ms
单点时限:1000...
有若干堆石子,每堆石子的数量都是有限的,合法的移动是“选择一堆石子并拿走若干颗(不能不拿)”,如果轮到某个人时所有的石子堆都已经被拿空了,则判负。
给出如下定义:
P-Positi...
Alice和Bob在玩这样一个游戏:给定k个数字a1,a2…ak。一开始,有x枚硬币,Alice和Bob轮流去硬币。每次所取硬币的枚数一定要在a1~ak中。Alice先取。取走最后一枚...
这道题是Nim博弈的变形。网上没有找到相关的题解,估计大部分人都没有看hihocoder里边所给出的提示,提示已经很清楚明确的把这道题变为Nim博弈问题了。
首先我们来分析一下这道题,首先我们不妨设所...
Time Limit:
MS (Java/Others)
Memory Limit:
K (Java/Others)
Total Subm...
他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 初中数学问题求教 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信