Factorial Trailing Zeroes
Factorial Trailing Zeroes
Total Accepted: 18149 Total Submissions: 66015Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Have you met this question in a real interview?
Yes
No
个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的,所以计算5的因子数,例如
100,100/5=20,20/5=4,4/5=0,就有20+4=24个0,因为每5个数有一个数可以被5整除,这些数中又每5个数可以被5整除。。。。
class Solution {
public:
int trailingZeroes(int n) {
int Count=0;
while(n){
Count+=n/5;
n/=5;
}
return Count;
}
};版权声明:本文为博主原创文章,未经博主允许不得转载。

浙公网安备 33010602011771号