Given an integer n, return the number of trailing zeroes in n!.

Example 1:

Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.

Example 2:

Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.

考虑n!的质数因子。后缀0总是由质因子2和质因子5相乘得来的。如果我们可以计数2和5的个数,问题就解决了。考虑下面的例子:

n = 5: 5!的质因子中 (2 * 2 * 2 * 3 * 5)包含一个5和三个2。因而后缀0的个数是1。

n = 11: 11!的质因子中(2^8 * 3^4 * 5^2 * 7)包含两个5和三个2。于是后缀0的个数就是2。

我们很容易观察到质因子中2的个数总是大于等于5的个数。因此只要计数5的个数就可以了。那么怎样计算n!的质因子中所有5的个数呢?一个简单的方法是计算floor(n/5)。例如,7!有一个5,10!有两个5。除此之外,还有一件事情要考虑。诸如25,125之类的数字有不止一个5。例如,如果我们考虑28!,我们得到一个额外的5,并且0的总数变成了6。处理这个问题也很简单,首先对n÷5,移除所有的单个5,然后÷25,移除额外的5,以此类推。下面是归纳出的计算后缀0的公式。

n!后缀0的个数 = n!质因子中5的个数
= floor(n/5) + floor(n/25) + floor(n/125) + ....
 class Solution {
public int trailingZeroes(int n) {
int cnt =0 ;
while(n>0){
cnt+=n/5;
n/=5;;
}
return cnt;
}
}
 

172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)的更多相关文章

  1. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

  2. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

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

  3. [LeetCode] Factorial Trailing Zeroes 阶乘末尾0

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

  4. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

    数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...

  5. 172 Factorial Trailing Zeroes 阶乘后的零

    给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...

  6. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

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

  7. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  8. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

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

  9. 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

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

随机推荐

  1. 为gitlab10.x增加使用remote_user HTTP头的方式登录

    项目的结构是这样的: 客户端通过Apache来访问后端的gitlab(gitlab的版本是10.4,手动从源码安装的简体中文版) , Apache作为gitlab的反向代理服务器 Apache内置了C ...

  2. C# 递归与非递归算法与数学公式

    1.递归 递归:程序调用自身的编程技巧称为递归(recursion). 优点是:代码简洁,易于理解. 缺点是:运行效率较低. 递归思想:把问题分解成规模更小,但和原问题有着相同解法的问题. 1)下面是 ...

  3. [Key] RegCure Pro

    Serial RegCure Pro  : 4A803-C4F23-422B6-1F3D6 http://www.paretologic.com/product/regcure-pro/

  4. linux下模拟CPU占用100%小程序

    在做一个测试时,需要模拟服务器CPU占用满的情况,在查阅相关资料后,发现网上程序不太好用, 原文在这:http://www.2cto.com/os/201304/202068.html 优化后如下: ...

  5. Razon模板

    转:http://www.cnblogs.com/huangxincheng/p/3644313.html 随笔- 119 文章- 0 评论- 2039 抛弃NVelocity,来玩玩Razor   ...

  6. POJ-2029 Get Many Persimmon Trees(动态规划)

    Get Many Persimmon Trees Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3987 Accepted: 2 ...

  7. 字符串-回文-Manacher算法

    http://blog.csdn.net/zzkksunboy/article/details/72600679 https://segmentfault.com/a/1190000008484167 ...

  8. 《ACM-ICPC程序设计系列 数论及其应用》例题个人答案记录

    例1.1:HDU2099(2017/9/4) 本题书上给的答案是从0到99枚举,显然可以优化到每次递增b,这样至少可以把枚举次数减少到1/10. #include<cstdio> int ...

  9. mybatis中大于等于、小于等于的写法

    在xml格式中,常常会遇到xml解析sql时候出错,这个时候需要用其他符号来表示.在mybatis中会遇到,需要做如下的转换:

  10. 洛谷P4168 蒲公英 [Violet] 分块

    题解:分块+离散化 解题报告: 一个分块典型题呢qwq还是挺妙的毕竟是道黑题 然,然后发现忘记放链接了先放链接QAQ 有两三种解法,都港下qwq 第一个是O(n5/3)的复杂度,谢总说不够优秀没有港, ...