Trailing Zeros
题目:
Write an algorithm which computes the number of trailing zeros in n factorial.
分析:
阶乘的实质是,乘几次5的倍数就多几个0,所以,只要看n底下有多少个5作为因子,就知道了。
很无聊的一道背诵题。
解法:
class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
long sum = 0;
while (n != 0) {
sum += n / 5;
n /= 5;
}
return sum;
}
};