public class Solution {
public int TrailingZeroes(int n) {
if (n == )
{
return ;
}
else
{
var x = n / ;
var y = TrailingZeroes(x);
return x + y;
}
}
}

https://leetcode.com/problems/factorial-trailing-zeroes/#/description

此类问题很显然属于数学问题,一定要找到其中的本质规律才能得到正确的数学模型。
两个大数字相乘,都可以拆分成多个质数相乘,而质数相乘结果尾数为0的,只可能是2*。如果想到了这一点,那么就可以进一步想到:两个数相乘尾数0的个数其实就是依赖于2和5因子的个数。又因为每两个连续数字就会有一个因子2,个数非常充足,所以此时只需要关心5因子的个数就行了。
对于一个正整数n来说,怎么计算n!中5因子的个数呢?我们可以把5的倍数都挑出来,即:
令n! = (*K) * (*(K-)) * (*(K-)) * ... * * A,其中A就是不含5因子的数相乘结果,n = *K + r(<= r <= )。假设f(n!)是计算阶乘n!尾数0的个数,而g(n!)是计算n!中5因子的个数,那么就会有如下公式:
f(n!) = g(n!) = g(^K * K! * A) = K + g(K!) = K + f(K!),其中K=n / (取整数)。
很显然,当0 <= n <= 4时,f(n!)=。结合这两个公式,就搞定了这个问题了。举几个例子来说: f(!) = + f(!) =
f(!) = + f(!) =
f(!) = + f(!) =
f(!) = + f(!) = + + f(!) =
f(!) = + f(!) = + + f(!) = + + f(!) = + + f() =

以上解释参考地址:https://www.cnblogs.com/kuliuheng/p/4102917.html

python的实现:

 class Solution:
def trailingZeroes(self, n: int) -> int:
count =
while (n > ):
count += n //
n = n //
return count

leetcode172的更多相关文章

  1. LeetCode----172. Factorial Trailing Zeroes(Java)

    package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...

  2. 每天一道LeetCode--172. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  4. leetcode172 阶乘后的零

    对数算法:O(nlogn) /** 即为统计0-n中5,10,15,20,25的个数,因为肯定有足够的偶数使得存在x*5=10*n,25=5*5因此计数加2,5=1*5计数加一: 但如果挨个计数当n很 ...

  5. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  6. LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

    172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...

  7. leetcode探索中级算法

    leetcode探索中级答案汇总: https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/ 1)数 ...

随机推荐

  1. Http缓存知识;HTTPS, HTTP2相关知识;百度统计和即时线上客服。

    安装 : 百度统计 来统计用户流量, Intercom 来做即时线上客服. 这两个是 JavaScript 插件放在 HTML 上的. HTTP缓存: https://developers.googl ...

  2. hdu 4845 状压bfs(分层思想)

    拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  3. HDU1598 并查集+贪心

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. 直方图及low_value、high_value

    直方图 Histogram是一种特殊的列统计信息,详细描述了目标列的数据分布情况.存储在数据字典基表 histogram$; 专门为了准确评估分布不均匀的目标列的可选择率.结果集的cardianlit ...

  5. POJ 2409 Let it Bead (Polya定理)

    题意 用k种颜色对n个珠子构成的环上色,旋转翻转后相同的只算一种,求不等价的着色方案数. 思路 Polya定理 X是对象集合{1, 2, --, n}, 设G是X上的置换群,用M种颜色染N种对象,则不 ...

  6. @pathVariable的作用(二十二)

    spring mvc中的@PathVariable是用来获得请求url中的动态参数的,十分方便,复习下: @Controller public class TestController { @Requ ...

  7. C语言对表达式的求值顺序不是明确规定的

    讨论区看到的 WA来自那些递归下降求解的代码. 第一种情况,使用|| 和 &&: 例如s为所给串 int getval() { switch(s[c_s++]) { case 'p': ...

  8. poj 3744 概率dp 快速幂 注意排序 难度:2

    /* Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5304   Accepted: 1455 De ...

  9. MVC Ajax Helpers

    在MVC中要实现Ajax有很多的方式,有微软自己的MicrosoftAjax,也可以用JQuery的AJax来实现,如果对其他的JavaScript框架熟悉,还可以采用其他的实现方案,比如说Proto ...

  10. Python3 字典Dict(十三)

    Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 字典是另一种可变容器模型,且可存储任意类 ...