Leetcode 172 Factorial Trailing Zeroes

给定一个数n 求出n!的末尾0的个数。

n!的末尾0产生的原因其实是n! = x * 10^m

如果能将n!是2和5相乘,那么只要统计n!约数5的个数.

1 class Solution {
2 public:
3       int trailingZeroes(int n) {
4         int ans = 0;
5         for( ;n; ans+=n/5,n/=5);
6         return ans;
7     }
8 };

 

posted @ 2016-03-04 22:26  Breeze0806  阅读(134)  评论(0编辑  收藏  举报