Nam Game
哪一方最终给对方留下4的倍数个石头则win,即想方设法的给对方留下4的倍数个石头。
例如: 9(B win)
A:3
B:2(B取2,给对方余4,对方则lose)
A:1 | 2 | 3
B:3 | 2 | 1(B win)
例如:9 (A win)
A:1 (留8,4的2倍)
B:1 | 2 | 3 (余下7 | 6 |5)
A:3 | 2 | 1 (余下4,4的1倍)
B:1 | 2 | 3 (余下3 | 2 | 1)
A:3 | 2 | 1 (A win)
当先手一方掌握了可以给对方留下4的倍数的权力,那么这一方就可以稳赢,否则则输
所以python代码实现:
"""
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.
"""
class Solution(object):
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
if n % 4 == 0 :
return False
else:
return True
Nam Game的更多相关文章
- NS2 nam中节点及数据流颜色设置
NS2 节点颜色设置在http://hi.baidu.com/jrwen0/item/d105c642f4c3ce36fb89601b说明的比較具体,大家能够參见. 我这里想说的是数据流颜色的设置,相 ...
- Ubuntu 16——安装——ns2.35和nam
Ubuntu 16.04 安装ns2.35+nam 总结出以下安装步骤 1: 更新源 sudo apt-get update #更新源列表 sudo apt-get upgrade #更新已经安装的包 ...
- NS2的LEACH仿真出来的nam文件拓扑的节点为什么x=0,且y=0
查看.tr文件和.nam发文件下所有的节点的x,y值都是(0,0),nam图像更没有运行出来 于是我将if { $opt(sc) == "" } {puts "*** N ...
- 已知有一个Worker 类如下: public class Worker { private int age; private String name; private double salary; public Worker (){} public Worker (String nam
package homework006; public class Worker { private int age; private String name; private double sala ...
- Visual studio 2013 bug:visual studio no editoroptiondefinition export found for the given option nam
昨天VS 2013打开项目,双击cs当文件,下面出现bug. Google没有理由.最后,在刚刚好清理C磁盘缓存用户文件夹. 然后就OK了. 详细的路径是:C:\Users\{当前用户}\AppDat ...
- visual studio no editoroptiondefinition export found for the given option nam
今天用VS 2012打开项目,打开项目时,出现以下的bug.解决方法:清理了在C盘用户文件目录下的缓存. 具体的路径是:C:\Users\{当前用户}\AppData\Local\Microsoft\ ...
- 前端Firebug常见错误:SyntaxError:missing variable nam
出现上面那个问题应该是 某个地方,分号写错了 检查一下是否由于应该写分号的地方写成了其他符号.
- Mysql - 函数
Mysql提供的函数是在是太多了, 很多我都见过, 别说用了. 园子里面, 有人弄了一个比较全的. MYSQL函数 我这里会将他写的完全拷贝下来, 中间会插入一些自己项目中使用过的心得 一.数学函数 ...
- ORACLE中STATUS为INACTIVE但是SERVER为SHARED状态的会话浅析
我们知道当ORACLE数据库启用共享服务器模式时,通过共享服务器模式连接到数据库的会话是有一些特征的.在v$session里面,其SERVER的状态一般为SHARED和NONE, 为SHARED时,表 ...
随机推荐
- Bootstrap~多级导航(级联导航)的实现
回到目录 在bootstrap官方来说,导航最多就是两级,两级以上是无法实现的,大叔找了一些第三方的资料,终于找到一个不错的插件,使用上和效果上都还不错,现在和大家分享一下 插件地址:http://v ...
- [C#反射]C#中的反射解析及使用.
1.对C#反射机制的理解2.概念理解后,必须找到方法去完成,给出管理的主要语法3.最终给出实用的例子,反射出来dll中的方法 参考: C#反射,MSDN编程指南 反射是一个程序集发现及运行的过程,通过 ...
- EXCEL的导出
具体实现代码: protected void EXCEL_Click(object sender, EventArgs e) { ToExcel(GridView1); } public void T ...
- VisualSVN-Server 安装以及使用教程
VisualSVN Server是免费的(代码管理工具),VisualSvn Server是SVN的服务器端 官网下载地址 安装VisualSVN-Server示例: 下载完安装包后,以安装Visua ...
- Hidden File For Mac
显示所有隐藏文件的方法: terminal中输入: defaults write com.apple.finder AppleShowAllFiles -bool true 同理,再次隐藏就输入: d ...
- 【WP8.1开发】基于应用的联系人存储
上一篇文章所吹的牛是访问系统(手机)上的联系人,当然那只是读不能改,这是自然的,要是让你能随便修改用户的联系人信息的话,那后果很严重,有些恶意开发者就有可能把”你的户口改成猪“. 但是,API也允许应 ...
- 【WP开发】认清“不透明度”与“可见性”的区别
这两种情况,许多朋友平时都没有注意到: 1.设置Opacity属性的值为0: 2.将Visibility属性设置为Collapsed. 不少人会简单地认为这两种情况是一样的,都是让UI元素看不见. 我 ...
- MySQL的学习--join和union的用法
感觉工作之后一直在用框架,数据库的一些基本的东西都忘记了,这次借着这个系列的博客回顾一下旧知识,学一点新知识. 今天就先从join和union开始. join 是两张表做交连后里面条件相同的部分记录产 ...
- GitHub入门教程 Hello World for GitHub
Intro 1.简介 What is GitHub? 2.什么是github? Create a Reposi ...
- svn 合并分支代码到主干
1. eclipse 安装subclipse 2. 将主干trunk导入到eclipse中 3. 右键选择team-->合并,注意,去掉红色圈内的选项 4. next 选中select查找svn ...