[CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

 

LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes

 

解法一:

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

 

解法二:

int trailing_zeros(int n) {
    return n == 0 ? 0 : n / 5 + trailing_zeros(n / 5);
}

 

CareerCup All in One 题目汇总

posted @ 2016-04-20 10:22  Grandyang  阅读(613)  评论(0编辑  收藏  举报
Fork me on GitHub