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

Note: Your solution should be in logarithmic time complexity.

转自:http://blog.csdn.net/doc_sgl/article/details/42344441

考虑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,以此类推。

总结:

只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种,5和5相乘的结果,所以,还要看n/5里面有多少个5,也就相当于看n里面有多少个25,还有125,625.。。

class Solution {
public:
int trailingZeroes(int n) {
int res = ;
while(n)
{
res += n/;
n /= ;
}
return res; }
};

Factorial Trailing Zeroes——数学类的更多相关文章

  1. 【LeetCode】172. Factorial Trailing Zeroes

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

  2. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  3. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

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

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

  5. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  6. 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 ...

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

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

  8. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  9. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

随机推荐

  1. 阅读android源码了解 android 加载so的流程

    参考原文:http://bbs.pediy.com/thread-217656.htm Android安全–linker加载so流程,在.init下断点: http://www.blogfshare. ...

  2. 【计数】【UVA11401】 Triangle Counting

    传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. O ...

  3. 分享一个JQuery弹出层插件

    JQuery插件TipsWindown 1.1 一个基于jQuery的弹出层.支持拖拽,支持内容为文字,图片,URL等!至于兼容性.在IE6下,弹出对像无法绝对固定.其他应该没啥大问题: 最新更新:( ...

  4. android 自定义控件---圆形方向盘

    在做Android平台开发的时候,经常会遇到安卓原生控件无法满足需求的情况,安卓允许开发者去继承已经存在的控件或者实现你自己的控件. 先来看一下效果图 采用直接集成View类,重写onDrow方法绘制 ...

  5. boost文件锁的使用

    boost中可以用boost::interprocess::file_lock类对文件进行加锁和解锁操作. #include <fstream> #include <iostream ...

  6. HDU 3487 Splay tree

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU 4313树形DP

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. javascript功能封装

    实现方式其实很简单,我在代码打上注释,大家就懂了!    var _date=[],dateData=["1月","2月","3月",&qu ...

  9. ACE服务端编程1:使用VS2010编译ACE6.0及从ACE5.6升级的注意事项

    ACE是一个跨平台的用于并发通信的C++框架,项目开始时使用的是ACE 5.6发布版,目前最新的ACE版本是6.3.0. 网上一直有一种黑ACE的氛围,主要黑点在于ACE的复杂和作者的背景,结合实际应 ...

  10. 在windows下如何使用密钥对远程登录服务器?

    在企业的生产中相信各位朋友都会使用远程登录服务器,这样即高效也非常方便,(服务器在西藏,没有远程技术,公司在北京,你只能到西藏与机器相伴,在这里我使用xshell软件),我们使用ssh 服务登录服务器 ...