Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial.
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的更多相关文章
- lintcode :Trailing Zeros 尾部的零
题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...
- [LeetCode] Factorial Trailing Zeros
Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
- [Algorithm] 2. Trailing Zeros
Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 1 ...
- 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 ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
- [LintCode] Trailing Zeroes 末尾零的个数
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- LeetCode(31)-Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 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 = ...
随机推荐
- 用 Docker 快速配置前端开发环境
来源于:http://dockone.io/article/1714 今天是你入职第一天. 你起了个大早,洗漱干净带着材料去入职. 签了合同,领了机器,坐到工位,泡一杯袋装红茶,按下开机键,输入密码, ...
- Oracle添加数据文件创建表空间,创建用户代码
1,添加数据文件创建表空间 CREATE TABLESPACE "TEST1" DATAFILE 'D:\ORACLE\11G\ORADATA\ORCL\TEST1.DBF' SI ...
- VS中两个常用辅助工具
一. 首当推荐的是DPack 下载地址:http://www.usysware.com/dpack/ 快捷键:以下都是个人常用的热键.其他还有,我都用得比较少了,3个已经完全够了 Alt+U 查找 ...
- ES6(ECMAScript 2015) 编码规范与详细注意要点
本规范是基于JavaScript规范拟定的,只针对ES6相关内容进行约定 如变量命名,是否加分号等约定的请参考JavaScript规范 应注意目前的代码转换工具(如Babel,Traceur)不够完善 ...
- Windows下python的配置
Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...
- BZOJ 3110 [Zjoi2013]K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- .net mvc4 利用 kindeditor 上传本地图片
http://blog.csdn.net/ycwol/article/details/41824371?utm_source=tuicool&utm_medium=referral 最近在用k ...
- JDK,JRE,JVM区别与联系(ZZ)
http://www.cnblogs.com/hencehong/p/3252166.html 我们开发的实际情况是:我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JD ...
- VirtualBox安装debian的详细方法步骤
下面是用VirtualBox安装Debian6的方法和步骤 l 新建一个文件夹,用于存放虚拟硬盘,如Debian l 打开VirtualBox,点击新建 l 输入虚拟机名称,Debian_6 l 给虚 ...
- UVa OJ 180 - Eeny Meeny
Time limit: 3.000 seconds限时3.000秒 Problem问题 In darkest <name of continent/island deleted to preve ...