172. 阶乘后的零
为保证权益,题目请参考 172. 阶乘后的零(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
using namespace std;
class Solution {
public:
int trailingZeroes(int n) {
int ans = 0;
for (int i = 5; i <= n; i++) {
int j = i;
while (j % 5 == 0) {
ans += 1;
j = j / 5;
}
}
return ans;
}
};
void runTest(){
Solution so;
cout << so.trailingZeroes(30) << endl;
}
int main() {
runTest();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Python
python
# 172. 阶乘后的零
# https://leetcode.cn/problems/factorial-trailing-zeroes/
class Solution:
def trailingZeroes(self, n: int) -> int:
ans = 0
while n != 0:
ans += (n := n // 5)
return ans
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10