Factorial Trailing Zeroes

来源:互联网 发布:matlab caffe win7 编辑:程序博客网 时间:2024/06/08 07:50

该题想了好久写了几个版本都超时,怒看解答,发现这更多的是个数学题。突然很烦leetcode上那个叫@ts的人。好几题经过语言包装的数学题都是这哥们提供的。。。

其实该题剥开外衣,问的就是有几个5。。。因为只有5X2才能产生0,而2个数一定大于5的,所以有几个5就有几个0.

代码如下:

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


0 0
原创粉丝点击