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. 三十三、MySQL 导入数据

    MySQL 导入数据 本章节我们为大家介绍几种简单的 MySQL 导出的数据的命令. 1.mysql 命令导入 使用 mysql 命令导入语法格式为: mysql -u用户名 -p密码 < 要导 ...

  2. 自动化运维工具——ansible命令使用(二)

    一.Ansible系列命令使用 ansible命令执行过程 1 . 加载自己的配置文件 默认/etc/ansible/ansible.cfg 2 . 加载自己对应的模块文件,如command 3 . ...

  3. tcl之内容

  4. 01 python爬虫

    ---

  5. 第48课 thinkphp5添加商品库

    目录 思路: 1. html页面里属性下拉框里的值是从goods_attr联attr里的查出来传到模板里的.在属性的下拉栏里展示出来 2. html页面里用二维数组的结构goods_attr[{$k} ...

  6. 【python学习】新手基础程序练习(二)

    Ι 继续上一节得内容,这里主要是对各种知识的理解以及如何运用. 一.执行 Python 脚本的两种方式 1.把python执行文件加到计算机的环境变量中,然后新建文件把程序写在新文件里,再通过cmd命 ...

  7. LED室内定位算法:RSS,TOA,AOA,TDOA(转载)

    转载自:https://blog.csdn.net/baidu_38197452/article/details/77115935 基于LED的室内定位算法大致可以分为四类: 1. 几何测量法 这种方 ...

  8. A1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  9. 自我介绍&友链

    目录 此博客 我是谁? 友链 此博客 此博客主要更新一些题解什么的...(随缘啦) 本蒟蒻太菜了,博客中出现了错误希望指出,谢谢! . . 我是谁? SD的一名蒟蒻!emm... 喜欢摸鱼. 喜欢抽卡 ...

  10. python 二——函数、装饰器、生成器、面向对象编程(初级)

    本节内容 1.函数 2.装饰器 3.生成器 4.类 一.函数 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 函数式 ...