aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-252/e-choroba/cpp/ch-1.cpp21
-rw-r--r--challenge-252/e-choroba/cpp/ch-2.cpp41
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-252/e-choroba/cpp/ch-1.cpp b/challenge-252/e-choroba/cpp/ch-1.cpp
new file mode 100644
index 0000000000..ec85e36bf0
--- /dev/null
+++ b/challenge-252/e-choroba/cpp/ch-1.cpp
@@ -0,0 +1,21 @@
+#include <cassert>
+#include <cmath>
+#include <iostream>
+#include <vector>
+
+long special_numbers(std::vector<long> ints) {
+ size_t s = ints.size();
+ long r = 0;
+ for (size_t i = 0; i <= s; ++i) {
+ if (s % (i + 1) == 0) {
+ r += std::pow(ints[i], 2);
+ }
+ }
+ return r;
+}
+
+int main(int argc, char* argv[]) {
+ assert(special_numbers({1, 2, 3, 4}) == 21);
+ assert(special_numbers({2, 7, 1, 19, 18, 3}) == 63);
+ std::cout << "Ok." << std::endl;
+}
diff --git a/challenge-252/e-choroba/cpp/ch-2.cpp b/challenge-252/e-choroba/cpp/ch-2.cpp
new file mode 100644
index 0000000000..e10d27aae6
--- /dev/null
+++ b/challenge-252/e-choroba/cpp/ch-2.cpp
@@ -0,0 +1,41 @@
+#include <cassert>
+#include <iostream>
+#include <vector>
+
+std::vector<int> unique_sum_zero(int n) {
+ std::vector<int> v;
+ if (n % 2) {
+ v = {0};
+ }
+ for (int i = 1; i <= n / 2; ++i) {
+ v.insert(v.end(), {i, -i});
+ }
+ return v;
+}
+
+int main(int argc, char* argv[]) {
+
+ auto u1 = unique_sum_zero(1);
+ assert(u1.size() == 1);
+ assert(u1[0] == 0);
+
+ auto u2 = unique_sum_zero(2);
+ assert(u2.size() == 2);
+ assert(u2[0] == 1);
+ assert(u2[1] == -1);
+
+ auto u3 = unique_sum_zero(3);
+ assert(u3.size() == 3);
+ assert(u3[0] == 0);
+ assert(u3[1] == 1);
+ assert(u3[2] == -1);
+
+ auto u4 = unique_sum_zero(4);
+ assert(u4.size() == 4);
+ assert(u4[0] == 1);
+ assert(u4[1] == -1);
+ assert(u4[2] == 2);
+ assert(u4[3] == -2);
+
+ std::cout << "Ok." << std::endl;
+}