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. BZOJ3118 : Orz the MST

    对于树边显然只需要减少权值,对于非树边显然只需要增加权值 设i不为树边,j为树边 X[i]:i增加量 X[j]:j减少量 C[i]:修改1单位i的代价 对于每条非树边i(u,v),在树上u到v路径上的 ...

  2. Codeforces Round #192 (Div. 2) A. Cakeminator

    #include <iostream> #include <vector> using namespace std; int main(){ int r,c; cin > ...

  3. 小结:kmp

    复杂度: O(len(a)+len(b)) 技巧及注意: 在匹配的时候记住先要自身匹配然后再匹配即可,同时边界问题不能忽略,处理好点吧. #include <cstdio> #includ ...

  4. Django 1.6.0 正式发布,大幅改进事务处理

    Django 1.6.0 正式发布了,查看官方发行说明. 下载地址:Django-1.6.tar.gz ,也可通过 pip 安装: pip install Django==1.6 要求 Python ...

  5. Android之adb异常

    Android使用adb模拟器时出现"unable to establish connection to adb"."ADB server didn't ACK" ...

  6. Java_获取当前月最后一天

    List<String> ms = DateUtils.getMonths(7,"yyyyMM"); SimpleDateFormat sdf = new Simple ...

  7. C++ Generate Rand Number Array by "srand()" 生成随机数

    在C++中,我们有时想生成一个由随机数组成的数组,而且随机数的范围也可由我们来设定.那么我们就要用到srand()函数配合rand()来使用,参见如下代码: #include <vector&g ...

  8. Resources

    McGuire Computer Graphics Data http://mesh.brown.edu/calibration/software.html Pixar Online Library ...

  9. for循环的时候是按照数字递增会造成一些元素被遗漏

    $test_array=array(1,2,3,4,4,5,5,6); $test_array_unique=array_unique($test_array); print_r($test_arra ...

  10. XML基础知识

    1.XML:(entension mark language) 可扩展的标记语言格式化的文本文件html(Hype Text Mark Language)(超文本标记语言)主要用于显示2.特点 一个良 ...