110(A,cpp): solve “Nearly Lucky Number”

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-25 23:03:27 +02:00
parent 7ec83aafc4
commit 403112b61e
Signed by: mfocko
GPG key ID: 7C47D46246790496
2 changed files with 1142 additions and 0 deletions

107
110/a.cpp Normal file
View file

@ -0,0 +1,107 @@
#include <cstdint>
#include <iostream>
namespace helpers {
using namespace std;
long pow(long base, long exp) {
if (exp == 0) return 1;
long half = pow(base, exp / 2);
if (exp % 2 == 0) return half * half;
return half * half * base;
}
template <typename T>
inline void answer(const T &ans) {
cout << ans << "\n";
}
inline void yes() { cout << "YES\n"; }
inline void no() { cout << "NO\n"; }
} // namespace helpers
namespace {
using namespace std;
using namespace helpers;
int count_lucky(uint64_t n) {
int lucky = 0;
for (; n > 0; n /= 10) {
if (n % 10 == 7 || n % 10 == 4) {
++lucky;
}
}
return lucky;
}
bool nearly_lucky(uint64_t n) {
auto count = count_lucky(n);
for (auto c = count_lucky(n); c > 0; c /= 10) {
if (c % 10 != 4 && c % 10 != 7) {
return false;
}
}
return count != 0;
}
void solve() {
uint64_t n;
cin >> n;
if (nearly_lucky(n)) {
yes();
} else {
no();
}
}
} // namespace
// for single test case, comment out for N test cases
#define SINGLE
#ifdef TEST
#include "../.common/cpp/catch_amalgamated.hpp"
TEST_CASE("examples") {
CHECK(!nearly_lucky(40047));
CHECK(nearly_lucky(7747774));
CHECK(!nearly_lucky(1000000000000000000));
}
TEST_CASE("regression (probably platform-dependant size of unsigned long)") {
CHECK(nearly_lucky(4744000695826));
}
#else
int main(void) {
#ifdef SINGLE
solve();
#else
// for multiple test cases
int N;
std::cin >> N >> std::ws;
for (auto i = 0; i < N; ++i) {
solve();
}
#endif
return 0;
}
#endif

1035
110/index.html Normal file

File diff suppressed because it is too large Load diff