question:

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.

my answer:

重点:

1 怎么计算,有些糊涂

2 python 除法

转自 https://segmentfault.com/a/1190000003912449

1.整数除法

对两个不能整除的整数做除法,就要面对舍入的问题。和大多数编程语言一样,Java的基本策略是向零取整(round to zero),也就是向绝对值变小的方向取整。举几个香甜的小栗子:3/2=1, -3/2=-1。而对于Python而言,情况就有所不同了。

>>>-1/10
-1

显然如果按照Java的取整策略,-1/10应该得0,而Python给出的结果是-1。事实上Python的取整方式是向下取整,也就是向着数轴上负无穷的方向取整。好吧,Java和Python的取整方式不同,夺大点事儿啊…那么如果我们要在Python下采用向零取整的结果,咋整?一种比较直接的方式是:

 
>>>int(float(-1)/10)
0

2.取余

谁说没大事?( ̄▽ ̄)大事来了!

Java和Python整数除法都遵循下面这个公式:

(a/b)*b+c=a

也就是说:

a mod b=c=a-(a/b)*b

这里的/表示的是整数除法。既然它们的取整方式不一样,那么取余也会受到影响:

 
For Java: -2 % 3==-2
For Python: -2 % 3==1

在某些实际应用中,我们可能会被要求得到一个整数的各位数字。如果输入的整数的正的,Java和Python都可以用相同的方法来解决:

def func(a):
pos, res=1, []
while a/pos:
res+=(a/pos)%10,
pos*=10
return res

Java代码也差不多就是这样了。但如果输入的整数是一个负数,Java版本的代码还是可以得到正确的结果,而Python不能(曾经在这里被坑的,举手)。那怎样用Python正确地搞定这个问题嘞?可以先去绝对值和符号,当正数来处理,最后再在结果里搭上符号。

3. Follow-ups

3.1 Python中的另一个除法操作

我们知道,在Python中,基本的除号“/”是被重载了的。当两个操作数都是整数时,进行整数除法,得到整数结果,否则进行浮点数除法(真除法),得到浮点数结果。从Python 2.2开始,另一个除号被引入://,它只执行整数除法。注意,//的结果类型依操作数而定。

>>>1.0/2
0.0
>>>1.0//2.0
0.0
>>>1//2
>0

另外,如果想同时得到商和余数,可以使用内建的函数divmod,结果是一个tuple。

>>>divmod(7, 2)
(3, 1)
>>>divmod(7.0, 2)
(3.0, 1.0)

3.2 Python中的舍入

除了缺省的舍入方式,Python还有多种舍入可供选择。
Floor rounding:

>>>import math
>>>math.floor(1.2)
1.0
>>>math.floor(-1.2)
-2.0

Ceiling rounding:

>>>math.ceil(1.2)
2.0
>>>math.ceil(-1.2)
-1.0

Round-off:

>>>round(0.5)
1.0
>>>round(-0.4)
-0.0
>>>round(-0.5)
-1.0

内嵌的round函数也可以一个指定保留小数位数的参数:

>>>round(0.21, 1)
0.2
>>>round(0.21, 2)
0.21

Caution !

>>>round(2.675, 2)
2.67

咦?bug啦?!当然不是。这里要明确一件事:计算机只认识0,1(量子计算机?懵)。就是说,我们输入的十进制数,在计算机内部都是用二进制来表示的。有的十进制数可以用二进制准确地表示出来,比如十进制的0.125可以表示为0b0.001;然而很多的小数是没法用二进制数精确表示的,计算机里存储的是它们的近似值,例如十进制的0.1,用二进制表示,可以近似为:0b0.00011001100110011001100110011001100110011001100110011010,所以当我们把它换回十进制数以输出或者使用,得到的值就是0.1000000000000000055511151231257827021181583404541015625。也就是说,0.1在计算机里并不是刚好等于1/10的。你看:

 
>>>0.1+0.2
0.30000000000000004

同样,当我们运行round()函数,也是对计算机中实际存储的值近似取舍。2.67实际上近似

2.67499999999999982236431605997495353221893310546875,第三位小数是4,那么round(2.675, 2)就相当于round(2.674, 2),结果当然是2.67。值得注意的是,这种现象是广泛存在于各种计算机和各种编程语言的,不是bug,只是有的语言选择了不让你看到。

leetcode笔记--3 Niim game的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

随机推荐

  1. Android(java)学习笔记48:通过反射获得带参构造方法并且使用

    1. 反射获得带参构造方法并且使用: (1)获取字节码文件对象       Class c = Class.forName("cn.itcast_01.Person"); (2)获 ...

  2. bzoj1264 [AHOI2006]基因匹配

    Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而成(地球上只有4种),而更奇怪的是,组成DNA序列的每一种碱 ...

  3. Python语言程序设计基础(3)—— 基本数据类型

    天天向上 dayup,dayfactor = 1.0,0.01 for i in range(365): if i % 7 in [6,0]: dayup = dayup*(1-dayfactor) ...

  4. POJ3737 UmBasketella

    嘟嘟嘟 一道三分入门题. 参考二分,三分就是每一次把区间分成三段,然后舍弃一段,不断缩小范围直到一个点. 一般用于求单峰函数的最值问题. 这道题发现V和r成一次函数的关系,因此三分r. 下面给出三分板 ...

  5. 【luogu P2319 [HNOI2006]超级英雄】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2319 #include <cstdio> #include <cstring> #i ...

  6. Android学习笔记_47_SIM卡介绍

    一.判断SIM卡属于哪个移动运营商 1.第一种方法:获取手机的IMSI码,并判断是中国移动\中国联通\中国电信 TelephonyManager telManager = (TelephonyMana ...

  7. LeetCode17.电话号码的字母组合 JavaScript

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  8. Java 依赖、关联、聚合和组合

    必须转一个,写的太好了! https://blog.csdn.net/zhengzhb/article/details/7190158

  9. MySQL数据库操作基础

    .MySQL 是什么? )软件(Software):工具(解决问题) )数据库管理系统(DBMS) )关系型(Relation)数据库管理系统(RDBMS):类似Oracle 扩展:db-engine ...

  10. MySQL 避免行锁升级为表锁——使用高效的索引

    文章目录 普通索引 属性值重复率高 属性值重复率低 小结 众所周知,MySQL 的 InnoDB 存储引擎支持事务,支持行级锁(innodb的行锁是通过给索引项加锁实现的).得益于这些特性,数据库支持 ...