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. Rails 5 Test Prescriptions 第8章 Integration Testing with Capybara and Cucumber

    Capybara:  A complete reference is available atrubydoc.info. 集成测试就是把局部的程序组合起来测试. 端到端测试是一个特殊的集成测试,覆盖了 ...

  2. Appium 自动化测试(6) -- 使用Appium操作YY语音例子

    #!/usr/bin/env python # -*- coding: utf-8 -*- import os import unittest from appium import webdriver ...

  3. bzoj2705: [SDOI2012]Longge的问题 欧拉定理

    题意:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 题解:考虑n的所有因子,假设有因子k,那么对答案的贡献gcd(i,n)==k的个数即gcd(i/k,n/k)== ...

  4. 本地Jdev Run PG报严重: Socket accept failed错误

    严重: Socket accept failed java.net.SocketException: select failed at java.net.PlainSocketImpl.socketA ...

  5. windows下jenkins安装过程中的那些坑

    在jenkins官网https://jenkins.io/download/下载2.89.4版本的war包,使用jar -jar jenkins.war命令安装,报端口被占的错误,使用jar -jar ...

  6. mysql 简单级联的学习

    数据库上面一直是我的弱项,昨天突然想到,简单的级联,即一个表中的列表删除了,另外一个依赖这个表的其他数据应该也会删除,当时想了下,可以根据外键来判断把其他表的数据给删除了,但是这样一来好像要必须知道其 ...

  7. (转)OAuth 2.0的设计思路

    OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...

  8. Python数据类型-01.数字和布尔值

    本节主要介绍Python中的基础知识中的数据类型,数字和布尔值 介绍几个知识点:1)内置函数print()的用法,直接打印括号里面的内容,或者print后跟多个输出,以逗号分隔.2)内置函数type( ...

  9. I.MX6 Android 5 root

    /********************************************************************************* * I.MX6 Android 5 ...

  10. mongodb下cpu高的查询方式(慢查询)

    1.查看mongodb进程 ps-ef | grep mongo 获取进程id为3267 2.查看进程的线程 top -p 3267 按shift+h 查看cpu高的线程,发现有线程点用cpu高且cp ...