Given 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.
https://leetcode.com/problems/factorial-trailing-zeroes/
1 package leetcode; 2 /*因为10=2*5,所以0的个数就是100!因式分解后2*5(必须配对)的个数.显然因子中2的个数比5多,因此问题划归为求解100!的因子中5的个数. 3 而1 2 3 4.....100这一百个数中5 10 15 20 .....100各含一个因子5(总共20个),25 50 75 100各含两个 4 (因为前面已经统计过一次,只能算一个) 5 这样答案就是20+4=24个0.*/ 6 public class Solution 7 { 8 public static int trailingZeroes(int n) 9 { 10 int cout=0; 11 if(n==0) 12 return cout; 13 for(int i=1;n>=Math.pow(5,i);i++) 14 { 15 cout+=n/Math.pow(5,i); 16 } 17 return cout; 18 } 19 public static void main(String args[]) 20 { 21 System.out.println(trailingZeroes(30)); 22 } 23 }