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

最初的代码

class Solution {
public:
int trailingZeroes(int n) {
long long int fac = 1;
int count=0;
if (n==0) fac = 1; for(int i = n;i>0;i--)
{
fac *= i ;
}
while(fac % 10 == 0)
{
count ++;
fac = fac/10;
}
return count;
}
};

报错:

Submission Result: Time Limit Exceeded

看了网上的才知道是质数分解,主要是提取5的个数:

class Solution {
public:
int trailingZeroes(int n) {
int ret = ;
for(int i = ;i<=n;i++)
{
int tem = i;
while(tem % == )
{
ret ++;
tem /= ;
}
}
return ret;
}
};

但是 提交后突然 OMG!!!

Last executed input:1808548329

再次在网上寻找原因继续想,进一步简化:

首先1~n中既有5的倍数,还有25的倍数,还是125的倍数等等,而25可以提供两个0,125可以提供3个0,(PS:我自己没有证明过,求大神给出证明,欢迎联系QQ:543451622)

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

Submission Result: Accepted

leetcode:Factorial Trailing Zeroes的更多相关文章

  1. LeetCode Day4——Factorial Trailing Zeroes

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

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

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

  3. LeetCode 172. 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

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

  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(easy)

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

  7. Java for LeetCode 172 Factorial Trailing Zeroes

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

  8. Java [Leetcode 172]Factorial Trailing Zeroes

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

  9. LeetCode(31)-Factorial Trailing Zeroes

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

随机推荐

  1. pingpang ball

    #pragma strict var smooth=2.0; var tiltAngle=30.0; function Start () { } function Update () { transf ...

  2. BZOJ1444 : [Jsoi2009]有趣的游戏

    建立AC自动机,并求出转移矩阵. 再用$\sum E(终止节点)=1$去替换第一个方程,高斯消元即可. 时间复杂度$O(n^3l^3)$. 注意精度问题,要特判0.00的情况. #include< ...

  3. BZOJ3749 : [POI2015]Łasuchy

    设f[i][S]表示第i份食物被两个人吃的状态为S是否有可能,枚举f[1][]的情况后检验 f[i][0]=(f[i-1][1]&a[i-1]>=a[i])|(f[i-1][3]& ...

  4. CentOS6.4 配置Nload监控网卡流量

    1.安装依赖包 yum install -y gcc gcc-c++ ncurses-devel make wget 2.下载Nload wget http://www.roland-riegel.d ...

  5. 【BZOJ】1441: Min(裴蜀定理)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1441 这东西竟然还有个名词叫裴蜀定理................ 裸题不说....<初等数 ...

  6. JAVA排序算法

    ];  ; i <  ; i++){ sort[i] = ran.nextInt(); } System.out.print(;i<sort.length;i++){ ;j<sort ...

  7. 关于UIWebView的总结

    关于UIWebView的总结 前言 今天参加了 Adobe 和 CSDN 组织的一个关于 PhoneGap 的开发讲座 ,而 PhoneGap 在 iOS 设备上的实现就是通过 UIWebView 控 ...

  8. Marching Cube

    GPU-Marching-Cubes An Implementation of the Marching Cubes[1] Algorithm Marching Cubes Matlab The St ...

  9. CreateFeatureClass COM异常

    private static IFeatureClass CreatStnShp(string shp) { //打开工作空间 IWorkspaceFactory wsfactory = new Sh ...

  10. js不能执行的几个小白错误

    1.如果使用jquery,而没有将js代码写在$(function(){}):里边,没等dom全部加载完,在执行时出现错误 2.如果感觉都写对了却没有执行,那么可能是在想要执行的函数前有一加载就能执行 ...