02 Number of trailing zeros of N!(求N的阶乘末尾0的数目)
2016-11-19 17:03
190 查看
题目描述
Write a program that will calculate the number of trailing zerosin a factorial of a given number.
N! = 1 * 2 * 3 * 4 … N
zeros(12) = 2 # 1 * 2 * 3 .. 12 = 479001600
that has 2 trailing zeros 4790016(00)
Be careful 1000! has length of 2568 digital numbers.
分析
最后一句话告诉我们1000!有2568个数字,所以你把结果算出来然后再得出来末尾0的数目是不可能的.所以还是要动下脑子的.不要总是暴力方式解决问题.注意到两点:只有2*5可以成为末尾0,而且乘法具有交换律,所以我们将能因式分解出2和5的乘数都因式分解,min(count(2),count(5))便为我们所求的答案了,然而显然count(2)要比count(5)要多得多,所以只需要求出count(5)就可以了,所以问题就简化成求count(5)
for (int i = 1; i <= n; i++) { int j=i; //因式分解 while (j%5==0){ count++; j=j/5; } }
over
相关文章推荐
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- leetcode解题之172# Factorial Trailing Zeroes Java版 (求n的阶乘末尾0的个数)
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
- LightOJ 1138 Trailing Zeroes (III) (阶乘末尾0的个数)
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
- 【LeetCode-面试算法经典-Java实现】【172-Factorial Trailing Zeroes(阶乘尾后0的数目)】
- Codewars --Number of trailing zeros of N!
- 求n的阶乘末尾有几个零
- n的阶乘末尾含0的个数
- UVa 10061 How many zero's and how many digits ? (任意进制下的阶乘长度和尾0的数目)
- 阶乘算法全集,阶乘末尾非零位,阶末尾零的个数(转)
- 计算阶乘n!末尾所含0的个数
- 计算阶乘n!末尾所含0的个数
- 给定一个整数N,那么N的阶乘N!末尾有多少个0呢?例如:N=10,N!=3 628 800,N!的末尾有两个0。
- 给定一个整数N,那么N的阶乘N!末尾有多少个0呢?