Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.算法
给定一个整数n,求阶乘结果的尾部0的个数。spa
假如i能够被5整除,则能够提供的5的个数为i/5个
假如i能够被25整除,则能够多提供的5的个数为i/25个
假如i能够被125整除,则能够多提供的5的个数为i/125个(算上了被5,25整除以后)
。。。.net
算法实现类code
public class Solution { // 假如i能够被5整除,则能够提供的5的个数为i/5个 public int trailingZeroes(int n) { int result = 0; long tmp = n; // 为了防止i*5超出int的表大表示范围 for (long i = 5; i <= tmp; i *= 5) { result += n / i; } return result; } }