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,题目实验便是使用了这个测试,所以越界的判断需要另外考虑。
 
代码如下:
  1. #include <iostream>
  2. #include <math.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. int trailingZeroes(int n) {
  9. int retCnt=,tmp5=;
  10. while(tmp5<=n){
  11. // cout<<tmp5<<" "<<n<<endl;
  12. retCnt+=n/tmp5;
  13. tmp5*=;
  14. if(tmp5%!=) break;
  15. }
  16. return retCnt;
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. Solution sol;
  23. // for(int i =1;i<=50;i++){
  24. // cout<<"i="<<i<<":"<<sol.trailingZeroes(i)<<endl;
  25. // }
  26. cout<<sol.trailingZeroes()<<endl;
  27. cout<<INT_MAX<<endl;
  28. return ;
  29. }
 

[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. 【Linux】linux 机器之间 zssh, rz, sz互相传输

    服务器端安装lrzsz: sudo  yum install lrzsz 本地客户端安装lrzsz: brew install lrzsz 本地客户端安装zssh: brew install zssh ...

  2. 本地已经存在的项目如何跟github发生关联

    切换到本地项目地址 git init 初始化项目.该步骤会创建一个 .git文件夹是附属于该仓库的工作树. git add . git commit -am 'initial commit' git ...

  3. 用jq给img添加error事件

    <img src="xxxx.jpg" alt="" /> <script> $(document).ready(function(){ ...

  4. 【TP】TP如何向模板中的js传变量

    <input type="hidden" class= "val" value = "{$value}" /> <scri ...

  5. ThinkPHP函数I代码优化

    ThinkPHP/Common/common.php 文件 I函数,主要用来获取一些gpc请求的变量的,函数有一部分代码是过滤变量的,每次都运行一次,其实是没有必要的. 如果你每次都像这样的方式调用的 ...

  6. django之配置静态文件

    # 别名 STATIC_URL = '/static/' # 配置静态文件,名字必须是STATICFILES_DIRS STATICFILES_DIRS = [ os.path.join(BASE_D ...

  7. Nearest Common Ancestors POJ - 1330 (LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34657   Accept ...

  8. 笔记-python-lib-内置函数

    笔记-python-lib-内置函数 注:文档来源为Python3.6.4官方文档 1.      built-in functions abs(x) 返回绝对值 all(iterable)   re ...

  9. 笔记-python-tutorial-5.data structure

    笔记-python-tutorial-5.data structure 1.      data structure 1.1.    list operation list.append(x) #尾部 ...

  10. javascript 实现九九乘法表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...