算法学习笔记--6.trailing-zeros

124 篇文章 0 订阅

设计一个算法,计算出n阶乘中尾部零的个数

样例

11! = 39916800,因此返回 2

挑战

O(logN)的时间复杂度

解答:

按照我这脑袋,先算阶乘,阶乘的结果一直用10去整除,能整除返回数值就加1。
然后第二个while循环出了问题,结果都运算不出来,天啊,还找不出怎么错的。
算法复杂度也远远超过log(n)。

困惑了一整天,终于发现是python3 中的除法出了问题,python3 中 / 变成了真除法,结果都用float型数据表示。程序中result = result/10 运行了一次while循环就停止了,天啊

换了python2 ,就能运行出结果了。

修改于2017/8/12

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 1/1
1.0
>>> 2/1
2.0
>>> 
class Solution:
    # @param n a integer
    # @return as a integer
    def trailingZeros(self, n):
        result, i = 1, 1
        while i <= n:
            result = result * i
            i += 1
        answer = 0
        while result % 10 == 0:
            answer += 1
            result = result/10
        return answer

更巧妙的解答:

n! = 1×2×3×4×…×n

将阶乘结果分解质因数相乘形式。

n!=1×2×3×5×...=K×5x

结尾为0,只能是2*5的形式,2,4,6,8等等都能产生2,5的数量决定了最后0的数量。
K看成不相关的质因数相乘(2 有很多,也包括在了里面)。
然后有多少个质因数5,阶乘的结果最后就会有多少个0。

算法中,1~n相乘。
每一个5的倍数能先得到一个质因数5。(n/5个,python自动取整)
25的倍数能再多得到一个质因数5。((n/5)/5个)
125的倍数能再多得到一个质因数5。
依次类推,直到最后,得到所有的质因数5的个数。

class Solution:
    # @param n a integer
    # @return as a integer
    def trailingZeros(self, n):
        result = 0
        while n != 0:
            n /= 5
            result += n
        return result

算法复杂度: O(n)=2×log5(n)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
xiazai.py:10:0: C0301: Line too long (130/100) (line-too-long) xiazai.py:29:21: C0303: Trailing whitespace (trailing-whitespace) xiazai.py:30:0: W0311: Bad indentation. Found 10 spaces, expected 12 (bad-indentation) xiazai.py:40:0: C0301: Line too long (103/100) (line-too-long) xiazai.py:41:0: C0301: Line too long (153/100) (line-too-long) xiazai.py:53:0: C0305: Trailing newlines (trailing-newlines) xiazai.py:1:0: C0114: Missing module docstring (missing-module-docstring) xiazai.py:7:0: C0103: Constant name "url" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:13:13: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) xiazai.py:14:16: I1101: Module 'lxml.etree' has no 'HTML' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member) xiazai.py:19:0: C0103: Constant name "num" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:21:4: R1723: Unnecessary "elif" after "break", remove the leading "el" from "elif" (no-else-break) xiazai.py:24:17: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) xiazai.py:25:20: I1101: Module 'lxml.etree' has no 'HTML' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member) xiazai.py:28:8: C0103: Constant name "judge" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:28:31: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:30:22: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:31:14: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:34:8: C0103: Constant name "chapter_num" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:38:29: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) xiazai.py:39:32: I1101: Module 'lxml.etree' has no 'HTML' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member) xiazai.py:41:22: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:42:16: C0103: Constant name "all_content" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:44:20: R1713: Consider using str.join(sequence) for concatenating strings from an iterable (consider-using-join) ----------------------------------- Your code has been rated at 5.43/10
07-15
根据你提供的代码检查结果,有一些问题需要注意和修正: 1. 第10行代码的长度超过了100个字符,建议将其分割成多行或缩短长度。 2. 第29行代码存在尾部空格,建议删除。 3. 第30行代码的缩进不正确,应该是12个空格而不是10个空格。 4. 第40行和第41行的代码长度超过了100个字符,建议进行分割或缩短长度。 5. 第53行代码存在多余的换行符,建议删除。 6. 第1行代码缺少模块文档字符串,建议添加以提供模块的说明。 7. 第7行和第19行的常量命名不符合大写命名规范,建议改为大写命名。 8. 第13行和第24行的`requests.get`方法缺少超时参数,可能导致程序无限期挂起,建议添加超时参数。 9. 第14行和第25行的`lxml.etree`模块没有`HTML`成员,可能需要将其添加到`extension-pkg-allow-list`中。 10. 第21行的`elif`语句在`break`之后是不必要的,建议删除`elif`关键字。 11. 第28行代码中的字符串格式化可以改为使用f-string进行简化。 12. 第30行和第31行的字符串格式化也可以改为使用f-string进行简化。 13. 第34行和第44行的常量命名不符合大写命名规范,建议改为大写命名。 14. 第38行和第39行的`requests.get`方法缺少超时参数,可能导致程序无限期挂起,建议添加超时参数。 15. 第39行的`lxml.etree`模块没有`HTML`成员,可能需要将其添加到`extension-pkg-allow-list`中。 16. 第41行的字符串格式化可以改为使用f-string进行简化。 17. 第42行的常量命名不符合大写命名规范,建议改为大写命名。 18. 第44行的字符串连接可以考虑使用`str.join(sequence)`来简化。 以上是对你提供的代码进行的静态分析和建议,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值