add: 220325-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-03-25 19:34:34 +08:00
parent 4b2864ed60
commit 8de2cc8b25
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 24 additions and 1 deletions

23
cpp/2203/220325-CN.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <iostream>
class Solution {
public:
// This algorithm is far NOT optimal.
static constexpr int trailingZeroes(int n) {
// Main algorithm: 2 * 5 = 10.
// Time complexity: O(n * sqrt(n))
int cnt2 = 0, cnt5 = 0;
for (int i = 2; i <= n; ++i) {
for (int x = i; !(x % 2); x /= 2)
++cnt2;
for (int x = i; !(x % 5); x /= 5)
++cnt5;
}
return std::min(cnt2, cnt5);
}
};
int main() {
std::cout << Solution::trailingZeroes(5);
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220325.cpp) ADD_EXECUTABLE(2203 220325-CN.cpp)