您的位置:首页 > 其它

lintcode 2:尾部的零

2017-12-13 21:10 435 查看

题目描述

设计一个算法,计算出n阶乘中尾部零的个数

分析

这其实是一个数学题,只有2和5这两个因子相乘才会在尾部产生0,2的数量比5的数量多得多,因此只需统计所有数中因子5有多少个就行了。

代码

public class Solution {
/*
* @param n: An integer
* @return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
// write your code here, try to do it without arithmetic operators.
long res = 0;
while(n>0){
res+=n/5;
n=n/5;
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: