Feb 3, 2020 ~ Feb 9, 2020
Problem 172.Factorial Trailing Zeroes(阶乘末尾的0) 题目链接
题目描述:给定一个整数n,求 n! 的末尾0的个数
思路为:因为 2 × 5 = 10,因此只要求得 n! 中因数 2 和 5 个数中的较小值,显然,因数为 5 的个数比因数为 2 的个数少。因为$ n! = 12...(n-1)n $,那么也就是求 1,2,...,n-1,n 这一列数中因数5的个数。我们发现只有 5, 10, 15, ... 的因数中才含有 5。因此我们容易想到通过计算 n//5 来求得因数等于5的个数,但这并没有全部算进去,因为像 25,50,125,625 这些数字不只含有一个因数5。因此,正确的求解公式如下:
\[ n! = \left \lfloor \frac{n}{5} \right \rfloor + \left \lfloor \frac{n}{5*5} \right \rfloor + \cdots + \left \lfloor \frac{n}{5 ^ {n-1}} \right \rfloor + \left \lfloor \frac{n}{5^n} \right \rfloor + \cdots = \left \lfloor \frac{n}{5} \right \rfloor + \cdots + \left \lfloor \frac{n}{5^k} \right \rfloor , 5^k \leq n < 5^{k+1} \]
通过的代码如下:
class Solution:
def trailingZeroes(self, n: int) -> int:
res = 0
tmp = 5
while n >= tmp:
res += (n // tmp)
tmp *= 5
return res
本周继续 Review 每个程序员需要知道的97件事(英文名:97 Things Every Programmer Should Know)。原文链接。下面是本周的5个小内容:
在 Python2 中,检查一个值是否为一个字典中的键中可以使用dict.has_key() 方法,而在 Python3 中,字典中没有该方法。无论 Python2 还是 Python3 都可以使用 in 关键字来进行检查某个键值是否存在。示例代码如下:
Python 2.7.14 (default, Oct 12 2017, 15:50:02) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {1:'a', 2:'b'}
>>> a
{1: 'a', 2: 'b'}
>>> a.has_key(1)
True
>>> 1 in a
True
Python 3.6.10 (default, Jan 16 2020, 09:12:04) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {1:'a', 2:'b'}
>>> a
{1: 'a', 2: 'b'}
>>> a.has_key(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'has_key'
>>> 1 in a
True
命令行工具是非常有用且强大的,它们通过提供许多的选项来使其具有强大的功能,有兴趣可以去看看 grep apt rpm 等命令的手册,它们都具有很多的选项。不过,正是由于其选项复杂,当不熟悉时,命令行使用起来没有 GUI 工具方便。GUI 工具也可以实现复杂的功能,但是很多使用 GUI 工具的人对其复杂的功能并不熟悉,就拿我个人而言,虽然在使用 IDEA,Visual Studio。但是,自己对这些工具并不熟悉,有一些功能总是在上网查询后才知道如何使用,并且一段时间不使用的话就很容易忘记了。
原文:https://www.cnblogs.com/mengxinayan/p/12288411.html