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. 【推荐】开放静态文件 CDN服务staticfile.org

    虽然国内外有很多类似的服务器,比如最初的google ajax api,还有后来的sae,百度等都有提供,但是也都有不同的弊端,比如国内访问速度慢.提供的静态文件不全等...staticfile有望解 ...

  2. telerik 某些ajax拿数据方式下 load on demand 不起作用

    方法一:加入以下代码 <%= Html.Telerik().ScriptRegistrar().DefaultGroup(group => { group.Add("teleri ...

  3. SecureCrt脚本(三)二级对象之Screen详解

    Crt自动化 测试 SecureCrt脚本 JS脚本   1.引言 2.Screen属性和方法 2.1.属性 2.1.1.CurrentColumn 2.1.2.CurrentRow 2.1.3.Co ...

  4. HttpClient_HttpClient 4.3.6 HTTP状态管理

    HTTP状态管理 最初的HTTP被设计成以状态.请求/应答为导向的协议,它被制作成是没有特殊条款的,以便在状态会话中能交换逻辑关系请求/应答.HTTP协议越来越受欢迎和被采用,越来越多的系统会在应用程 ...

  5. android ScrollView滚动距离和判断滚动停止状态

    今天很高兴,自己解决了判断ScrollView滚动停止的监听,现在分享给大家. 因为ScrollView没有像listView中的setOnScrollListener()监听,当然也就没有SCROL ...

  6. Angular数据双向绑定

    Angular数据双向绑定 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.Angul ...

  7. 使用System.out.print/prilntln() 输出时存在的问题

    刚学习Java时第一个接触的method就是System.out.println() 方法.但是最近在使用它输出一些变量时出现了我不理解的现象,首先上代码: /* * * using method S ...

  8. Stackoverflow架构

    Stackoverflow用的是.net开发的,用的缓存是Redis,Stackoverflow架构的演讲地址是:http://www.infoq.com/cn/presentations/archi ...

  9. linux下六大IPC机制【转】

    转自http://blog.sina.com.cn/s/blog_587c016a0100nfeq.html linux下进程间通信IPC的几种主要手段简介: 管道(Pipe)及有名管道(named ...

  10. Apache Spark技术实战之1 -- KafkaWordCount

    欢迎转载,转载请注明出处,徽沪一郎. 概要 Spark应用开发实践性非常强,很多时候可能都会将时间花费在环境的搭建和运行上,如果有一个比较好的指导将会大大的缩短应用开发流程.Spark Streami ...