n阶乘 尾数0的个数

class Solution {
public:

    int trailingZeroes(int n) {

           if(n<=0) return 0;

            int i=0;
           int res=0;

           while(n){
      res+=n/5;
      n=n/5;
     }
return res;
}
};

 

 

很神奇的,eg  125

125=25*5,相当于前前面有1,2,3,4,5,……,20,21,22,23,24,25  * 5

125/5 = 25 相当于前面变成0,0,0,0,1,……,4,4,4,4,4,5               *5

25/5=5  所以最后结果结果过就是25+5+1

posted @ 2015-07-16 21:11  *桔子*  阅读(232)  评论(0编辑  收藏  举报