Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解题思路:
计算n能达到的5的最大次幂,算出在这种情况下能提供的5的个数,然后减去之后递归即可,JAVA实现如下:
static public int trailingZeroes(int n) { if(n<25) return n/5; long five=5; int count=0; while(n>=five){ five*=5; count++; } int temp=(int) (n/Math.pow(5, count)); return countSum(count)*temp+trailingZeroes(n-temp*(int)Math.pow(5, count)); } static public int countSum(int count){ if(count==1) return 1; else return countSum(count-1)*5+1; }