Write an algorithm which computes the number of trailing zeros in n factorial.
Example
11! = 39916800, so the out should be 2
一开始没想明白,后来随手列了一些相乘尾数能等于0的数,2*5,4*5,5*6,5*8。发现都是与5相关。所以其实就是求5的数量
class Solution {
public:
/*
* @param n: A long integer
* @return: An integer, denote the number of trailing zeros in n!
*/
long long trailingZeros(long long n) {
// write your code here, try to do it without arithmetic operators.
long long sum=0;
while(n){
sum+=n/5;
n=n/5;
}
return sum;
}
};