aboutsummaryrefslogtreecommitdiff
path: root/challenge-085/ash/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-11-08 19:40:05 +0100
committerAbigail <abigail@abigail.be>2020-11-08 19:40:05 +0100
commit4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1 (patch)
tree52495c7853bf31c427ec26f500c66c5bed6753b4 /challenge-085/ash/cpp/ch-1.cpp
parent520db2cfca438cdc9a78932e5f479e28abee150b (diff)
parentff7475c4559b1087f402725a7c526d3f780e3434 (diff)
downloadperlweeklychallenge-club-4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1.tar.gz
perlweeklychallenge-club-4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1.tar.bz2
perlweeklychallenge-club-4e34dd5d0e89307aca9f4cd3bb6072c07f942ff1.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-085/ash/cpp/ch-1.cpp')
-rw-r--r--challenge-085/ash/cpp/ch-1.cpp40
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;
+}