Factorial Trailing Zeroes

来源:互联网 发布:男士控油保湿知乎 编辑:程序博客网 时间:2024/06/15 22:53

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

思路:实现很简单,关键的分析题意,要求n!末尾的0数,事实上可以求5和2的对数。而又因为出现5必定出现2,所以求5个数即可.

 int trailingZeroes(int n) {        int count  = 0;        n = n/5;        while(n>0)        {            count+=n;            n /=5;        }        return count;    }


 

0 0