diff options
| author | Andrew Shitov <andy@shitov.ru> | 2020-11-03 11:11:29 +0100 |
|---|---|---|
| committer | Andrew Shitov <andy@shitov.ru> | 2020-11-03 11:11:29 +0100 |
| commit | 3ec3091ca2b184401a324ff2916cbbd57358bd62 (patch) | |
| tree | 0db0d9d7673312d6b08f2017e248c002192f702a /challenge-085/ash/cpp | |
| parent | 2fdc043b7f7afa6206b4c66a8989907572baad37 (diff) | |
| download | perlweeklychallenge-club-3ec3091ca2b184401a324ff2916cbbd57358bd62.tar.gz perlweeklychallenge-club-3ec3091ca2b184401a324ff2916cbbd57358bd62.tar.bz2 perlweeklychallenge-club-3ec3091ca2b184401a324ff2916cbbd57358bd62.zip | |
085-1 in C++ by ash
Diffstat (limited to 'challenge-085/ash/cpp')
| -rw-r--r-- | challenge-085/ash/cpp/ch-1.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-085/ash/cpp/ch-1.cpp b/challenge-085/ash/cpp/ch-1.cpp new file mode 100644 index 0000000000..a3f533c450 --- /dev/null +++ b/challenge-085/ash/cpp/ch-1.cpp @@ -0,0 +1,40 @@ +/* + Task 1 from + https://perlweeklychallenge.org/blog/perl-weekly-challenge-085/#TASK1 + + $ g++ -std=c++17 ch-1.cpp + $ ./a.out + 1 +*/ + +#include <iostream> +#include <vector> + +using namespace std; + +int main() { + vector<double> r = {1.2, 0.4, 0.1, 2.5}; + // vector<double> r = {0.2, 1.5, 0.9, 1.1}; + + bool found = false; + for (unsigned int c = 0; c != 2 << (r.size() - 1); c++) { + unsigned int mask = c; + unsigned int unit_bits = 0; + double sum = 0; + for (unsigned int bitpos = r.size(); bitpos; bitpos--) { + unsigned int current_bit = mask & 1; + unit_bits += current_bit; + mask >>= 1; + + if (current_bit) sum += r[bitpos - 1]; + } + + if (unit_bits == 3 && sum > 1 && sum < 2) { + cout << 1 << endl; + found = true; + break; + } + } + + if (!found) cout << 0 << endl; +} |
