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

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

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. 大数相乘算法C++版

    #include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...

  2. Html-Css-div透明层剧中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 最短路算法floyd

    内容: 对n个点(n<=450),已知他们的边,也就是相邻关系,求任意两个点的最短距离. 代码: for(int k=1; k<=n; k++)//k写在外面 for(int i=1; i ...

  4. 【FE前端学习】第二阶段任务-基础

    技能学习部分: 1.需要熟练掌握HTML标签以及CSS各个常用属性. 2.掌握CSS3 常用属性 3.掌握jquery的基本用法,对于JS基本逻辑语句需要熟练掌握 上文 [FE前端学习]第二阶段任务- ...

  5. 查询条件Where

    1.字符串 $condition = 'name=\'Lily\' and age>10'; 2.数组 ['type' => 1, 'status' => 1] //生成 (type ...

  6. 第六节 JBPM版本控制以及Token对象

    1.JBPM版本 2.Token 3.流程上下文

  7. getHibernateTemplate()的用法

    getHibernateTemplate() spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作,此方法是在配置了spring以后,hi ...

  8. jstl是自动就有的吗,不是的Unknown tag (c:if).

    这个错误的原因就是没有导包 http://www.runoob.com/jsp/jsp-jstl.html 这个网站有方法

  9. 活用scanf

    scanf()是C语言中用于读入格式化数据(formatted data)的函数. 我们可能对scanf()的一般用法已经了然,而至于scanf()读入数据的机制恐怕并不十分清楚. 下面我将比较详细地 ...

  10. jQuery的插入

    append(content) 概述 : 向每个匹配的元素内部追加内容. 这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似 append(function(index ...