172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
计算n的阶乘后面有多少0
思路:就是看能被多少个10整除,而10又可以分解为2*5,两个素数乘积,而在阶乘中,2的个数远远大于5,就是有一个5必然有一个2,所以只要统计5的个数就可以了。
5的个数=(n/5 + n/5^2 + n/5^3......),n/5代表含1个5的数的个数,n/5^2代表含2个5的个数,相当于含两个5的数个数就加了2次,就是num(2个5)*2,以此类推。
代码:
class Soloution{
Public:
int trailingzeros(int n)
{
int count = 0;
while(n/5)>=1
{
count += n/5;
n = n/5;
}
return count;
}
}