原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/
求factorial后结尾有多少个0,就是求有多少个2和5的配对。
但是2比5多了很多,所以就是求5得个数。但是有的5是叠加起来的比如 25,125是5的幂数,所以就要降幂。
e.g. n = 100, n/5 =20, n/25= 4, n/125=0,所以加起来就有24个0.
AC Java:
public class Solution {
public int trailingZeroes(int n) {
//count number of 5 and 2, but 2 is much more than 5, so just count 5
int res = 0;
while(n>0){
res += n/5;
n /= 5;
}
return res;
}
}