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

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

Math

 

  这题应该是2014年年底修改该过测试样本,之前的通过遍历1-n 然后判断每个数5因子的个数,这个简单的遍历方法不行了,时间限制了。
  然后既然不能遍历,便只能换个思路,能够构成末尾0,其实决定因素在于1 to n  的数一共有多少个5因子。那么我们这样考虑:
对于5
  那么能够被他整除的是 5 10 15 25 30 ... 
 
  这样其实便一共有n/5,对于 25 50 这样的数包括了两个5因子,我们在后面会计算的,在考虑5的时候,结果便是 n/5。
 
对于25
  能够被整除的是 25 50 75 ...
     
     这样,其实一共有n/25个,这时候25 中的两个5因子,变都计数了。
 
对于 125
  同样能够被其整除的是125 625...
 
  这样,是不是结果其实便是:
n/5 + n/25 + n/125 ...
 
  这样计算有个问题,会越界,c++ int类型一直乘以5越界时候,会变成 1808548329,题目实验便是使用了这个测试,所以越界的判断需要另外考虑。
 
代码如下:
#include <iostream>
#include <math.h>
#include <vector>
using namespace std; class Solution {
public:
int trailingZeroes(int n) {
int retCnt=,tmp5=;
while(tmp5<=n){
// cout<<tmp5<<" "<<n<<endl;
retCnt+=n/tmp5;
tmp5*=;
if(tmp5%!=) break;
}
return retCnt;
}
}; int main()
{
Solution sol;
// for(int i =1;i<=50;i++){
// cout<<"i="<<i<<":"<<sol.trailingZeroes(i)<<endl;
// }
cout<<sol.trailingZeroes()<<endl;
cout<<INT_MAX<<endl;
return ;
}
 

[LeetCode] 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 Factorial Trailing Zeroes (阶乘后缀零)

    题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...

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

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

  4. LeetCode Factorial Trailing Zeroes Python

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

  5. ✡ 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 ...

  6. LeetCode Factorial Trailing Zeroes

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

  7. 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解

    题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...

  8. Python3解leetcode Factorial Trailing Zeroes

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

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

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

随机推荐

  1. http 实战练习

    http 实战练习 建立httpd服务器,要求提供两个基于名称的虚拟主机: (1)www.X.com,页面文件目录为/web/vhosts/x:错误日志为/var/log/httpd/x.err,访问 ...

  2. python之2.x与3.x区别(仅限于基础)

    因为看的是python2.x的书籍.用的是python 3.7.所以先把两者的区别记录一下,仅限于基础. 1.input python3.0之后,不区分input()和raw_input(),统一为i ...

  3. 配置vim nginx.conf高亮

    #!/bin/bashwget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vimmv nginx.vim ...

  4. thinkphp 5数据库操作

    1.原生sql $options=Db::table('__MALL_POST__') ->alias('m') ->join('__MALL_CATEGORY_VALUE__ v','m ...

  5. 动态规划:HDU1087-Super Jumping! Jumping! Jumping!(最大上升子序列和)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. 【转】Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本(一)

    Oracle 的AWR 报告能很好的提供有关DB性能的信息. 所以DBA 需要定期的查看AWR的报告. 有关AWR报告的说明参考: Oracle AWR 介绍 http://blog.csdn.net ...

  7. android/libs/libammsdk.jar" already exists! 解决方法

    Error: Uh oh!"/work/2016/fengkongbao/.meteor/local/cordova-build/platforms/android/libs/libamms ...

  8. easyui datagrid复选框控制单选

    使用easyui datagrid的时候,由于对数据表格操作太多,并且有单选和多选功能因此采用复选框.但是在单选的状态,使用CheckOnSelect和singleselect时发现,页面有明显延迟, ...

  9. 微服务化的不同阶段 Kubernetes 的不同玩法

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为容器集群管理技术竞争的大赢家,Kubernetes已经和微服务紧密联系,采用Kubernetes的企业往往都开始了微服务架构的探索.然而不同企业 ...

  10. Stephen 博客正式开通 【个人公众号:Stephen 】

    个人博客开通. 个人公众号:Stephen