172.阶乘后的零
链接:172.阶乘后的零
难度:Medium
标签:数学
简介:给定一个整数 n,返回 n! 结果尾数中零的数量。
题解 1 - javascript
- 编辑时间:2021-07-29
- 执行用时:72ms
- 内存消耗:39.1MB
- 编程语言:javascript
- 解法介绍:只有 2*5 才会出现 0,统计出现 5 的次数。
var trailingZeroes = function (n) {
let ans = 0;
let m = 5;
while (n / m) {
ans += ~~(n / m);
m *= 5;
}
return ans;
};
题解 2 - cpp
- 编辑时间:2022-03-25
- 内存消耗:6MB
- 编程语言:cpp
- 解法介绍:统计有几个 5。
class Solution {
public:
int trailingZeroes(int n) {
int ans = 0;
for (int cnt = 1; pow(5, cnt) <= n; cnt++) ans += n / pow(5, cnt);
return ans;
}
};