-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode172_factorial_trailing_zeroes.cpp
58 lines (50 loc) · 1.18 KB
/
leetcode172_factorial_trailing_zeroes.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @file leetcode172_factorial_trailing_zeros.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/14 19:18
* @brief https://leetcode.com/problems/factorial-trailing-zeroes/
*
**/
#include <iostream>
#include <queue>
#include <cmath>
class Solution {
public:
int trailingZeroes(int x) {
int n2 = 0;
int n5 = 0;
int n10 = 0;
for (int i = 1; i <= x; ++i) {
int j = i;
while (j % 10 == 0) {
++n10;
j /= 10;
}
while (j % 5 == 0) {
++n5;
j /= 5;
}
while (j % 2 == 0) {
++n2;
j /= 2;
}
}
int ret = n10;
int n = std::min(n2, n5);
ret += n;
return ret;
}
};
int main() {
while (1) {
std::cout << "Input integer (control-C to exit): ";
int x = 0;
if (!(std::cin >> x)) {
return 0;
}
Solution solution;
auto ret = solution.trailingZeroes(x);
std::cout << ret << std::endl;
}
return 0;
}