[Lintcode]2. Trailing Zeros
发布时间:2020-12-14 04:25:29 所属栏目:大数据 来源:网络整理
导读:2. Trailing Zeros 本题难度: Easy Topic: MathBit Manipulation Description Write an algorithm which computes the number of trailing zeros in n factorial. Example Example 1: Input: 11 Output: 2 Explanation: 11! = 39916800,so the output should
2. Trailing Zeros
Example Explanation: 11! = 39916800,so the output should be 2 Example 2: Explanation: 5! = 120,so the output should be 1. Challenge 我的代码class Solution: """ @param: n: An integer @return: An integer,denote the number of trailing zeros in n! """ def trailingZeros(self,n): # write your code here,try to do it without arithmetic operators. count = 0 f = 5 while(f<n): count += n//f f *= 5 return count 思路每一个末尾处为0都是有5和2相乘得出来的,2出现的频率远远高于5,所以不需要考虑2出现的情况。只需要考虑分解质因数出现了多少个5即可 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |