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 = ...
随机推荐
- OC基础--OC内存管理原则和简单实例
ARC: 由于自己的学习视频太早,Xcode是iOS6版本,新建命令行项目后,系统会默认启动ARC机制,全程Automatic Reference Counting,简单的说,就是代码中自动加入了re ...
- formData_html5_map标签
1 : //更省事 var files = fileInput.files; var formData = new FormData(); //将所有文件插入formData formData .ap ...
- Java基础-序列化
Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象. 序列化是Java中实现持久化存储的一种方法: 为数据传输提供了线路级对象表示法. Java的序列化机制是通过在运行时判 ...
- Java编程思想学习(九) 异常处理
java的异常处理机制可以使程序有极好的容错性,让程序更加的健壮.所谓的异常,就是指的阻止当前方法或作用域继续执行的问题,,当程序运行时出现异常时,系统就会自动生成一个Exception对象来通知程序 ...
- 手动搭建SpringMVC报错
猜测这个是由于自己在搭建时缺少包造成的,后来将按照自己之前的项目将包补齐,tomcat就不报错了,看来还是要学习maven 这样就不会缺少包了
- Sqlserver 读取EXCEL
1.1 启用本地读取设置 --启用EXEC sp_configure 'show advanced options', 1RECONFIGUREEXEC sp_configure 'Ad Hoc Di ...
- 洛谷P2327 [SCOI2005] 扫雷
题目描述 输入输出格式 输入格式: 第一行为N,第二行有N个数,依次为第二列的格子中的数.(1<= N <= 10000) 输出格式: 一个数,即第一列中雷的摆放方案数. 输入输出样例 输 ...
- NOIP2014 day2 T2 洛谷P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- 排序算法(一)(时间复杂度均为O(n*n))
对于一个int数组,请编写一个选择排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] 冒泡排序 ...
- HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...