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. 视频播放实时记录日志并生成XML文件

    需求描述: 在JWPlayer视频播放过程中,要求实时记录视频观看者播放.暂停的时间,并记录从暂停到下一次播放时所经过的时间.将所有记录保存为XML文件,以方便数据库的后续使用. 实现过程: 尝试1: ...

  2. [转载]VS2012编译C语言scanf函数error的解决方法

    在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may ...

  3. simple-LDAP-auth

    <?php /** * simple class for LDAP authentification * Copyright (C) 2013 Petr Palas This program i ...

  4. Cas_个人理解

        分为三个部分: 1.Cas服务器(用于验证用户是否正确)        1.用户信息存在服务端,其它客户端应用程序修改用户信息后需要同步到服务端       2.用户信息一般存储在服务端的数据 ...

  5. 【BZOJ-3651&3081】网络通信&StrangeRegulations Link-Cut-Tree

    3651: 网络通信 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 90  Solved: 61[Submit][Status][Discuss] D ...

  6. BZOJ-3524 Couriers 可持久化线段树

    可持久化线段树,其实就是类主席树了.. 3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1124 Sol ...

  7. javaScript基础练习题-下拉框制作

    1.基础回顾 如何让一个段javascript在文档加载后执行,(因为自己忘了,所以顺便复习一下) window.onload = function(){}; <!DOCTYPE html PU ...

  8. IIS7部署项目时提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败。"的解决办法

    这个错误的定位:你的站点使用了Forms验证,而且在部署在生产环境的时候,设置错误,或者注释了. 解决方法如下: 1.检查Forms配置是否屏蔽. 2.有权限访问的资源是否已经开发. 基本就围绕以上两 ...

  9. poj 1006 中国剩余定理解同余方程

    其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...

  10. POJ1065 Area

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18499   Accepted: 5094 Description You ...