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

分析:

  1.  //计算包含的2和5组成的pair的个数就可以了,一开始想错了,还算了包含的10的个数。  
  2.         //因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定。  
  3.         //观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以。  
  4.         //但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25=(5*5)的另外一个5),  
  5.         //所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, ..., n/5/5/5,,,/5直到商为0。  
    1 class Solution {
    2 public:
    3     int trailingZeroes(int n) {
    4          return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
    5     }
    6 };

     

posted on 2017-06-30 15:20  无惧风云  阅读(129)  评论(0)    收藏  举报