add: 220331-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-03-31 23:26:40 +08:00
parent 617be4da0a
commit 5807178936
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 40 additions and 1 deletions

39
cpp/2203/220331-CN.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <vector>
template<int N>
class AnswerTable {
public:
bool isNotAnswer[N]{};
explicit constexpr AnswerTable() {
for (int i = 1; i < N; ++i) {
for (int j = i; j; j /= 10) {
if (!(j % 10) || i % (j % 10)) {
isNotAnswer[i] = true;
break;
}
}
}
}
};
/**
* 728. Self Dividing Numbers
* A self-dividing number is a number that is divisible by every digit it contains.
* - For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
* A self-dividing number is not allowed to contain the digit zero.
* Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].
*/
class Solution {
private:
static constexpr AnswerTable<10001> ans = AnswerTable<10001>();
public:
static std::vector<int> selfDividingNumbers(int left, int right) {
std::vector<int> ret;
for (int i = left; i <= right; ++i)
if (!ans.isNotAnswer[i])
ret.push_back(i);
return ret;
}
};

View File

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