Write an algorithm which computes the number of trailing zeros in n factorial.

Have you met this question in a real interview?

Yes
Example

11! = 39916800, so the out should be 2

 /*
* param n: As desciption
* return: An integer, denote the number of trailing zeros in n!
我们会发现: the number of 2s in prime factors is always more than or equal
to the number of 5s. So if we count 5s in prime factors, we are done. How to count total number of 5s in prime factors of n!? A simple way is
to calculate floor(n/5). 问题转化为求阶乘过程中质因子5的个数,但是要注意25能提供2个5,125能提供3个5....
所以,count= floor(n/5) + floor(n/25) + floor(n/125) + ....
*/ public class Solution {
public int trailingZeroes(int n) {
if (n < ) return ; int r = ;
while (n > ) {
n /= ;
r += n;
}
return r;
}
}

Trailing Zeros的更多相关文章

  1. lintcode :Trailing Zeros 尾部的零

    题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...

  2. [LeetCode] Factorial Trailing Zeros

    Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...

  3. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

  4. [Algorithm] 2. Trailing Zeros

    Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 1 ...

  5. codewars--js--Number of trailing zeros of N!

    问题描述: Write a program that will calculate the number of trailing zeros in a factorial of a given num ...

  6. [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

    LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...

  7. [LintCode] Trailing Zeroes 末尾零的个数

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  8. LeetCode(31)-Factorial Trailing Zeroes

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

  9. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. jquery 插件之 点赞“+1” 特效

    一般用户点个赞后,都会有个 +1 的特效飘过,用户已经点过赞了,会有“已点过赞”的特效提示 在这里,我们写了一个点赞的插件 //扩展对象点赞插件.点赞特效 //用法:jQuery('.praisebt ...

  2. Yii2请求,报400错误

    出现400错误是yii2.0的csrf防范策略导致 在components里面添加request配置如下: 'request' => [ // !!! insert a secret key i ...

  3. javaIO(三)

  4. 学习使用Robot Framework自动化测试框架-web元素定位

    转:http://blog.csdn.net/u012145166/article/details/50342569 1.name和id 其中使用到了name和id定位.但有时候由于开发人员的疏忽或者 ...

  5. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  6. udp 内网穿透 互发消息

    还差实现内网终端,向服务器发送请求,要对方的内网连接自己,实现打洞.在同一网段,或者公网运行,可以相互聊天. 没有实现检测客户端下线功能. 1,服务器代码 package router; import ...

  7. MyEclipse------在特定目录创建文件和书写内容

    readwrite.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...

  8. yii 10.16

    什么是框架? a)         框架就是一个半成品,一个帮助我们完成业务流程的程序 b)        框架融合了很多的功能,但是不会给我们具体功能模块 c)         我们需要在框架的基础 ...

  9. java中不带package和带package的编译运行方式

    Java中不带package的程序和带package的程序编译的方式是不同的. 一.不带package的程序建立个HelloWorld.java的文件,放入C:\,内容如下:public class ...

  10. iOS-Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its super

    今天在XCode6.3上面重写TabBar的时候,自定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件中 警告消失.