From b4dc318d75475dd533c9a16289ce8c0f698865a8 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Wed, 30 Sep 2020 14:34:38 +0200 Subject: Solutions 080-1 in C++ and Raku --- challenge-080/ash/cpp/ch-1.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-080/ash/cpp/ch-1.cpp (limited to 'challenge-080/ash/cpp/ch-1.cpp') diff --git a/challenge-080/ash/cpp/ch-1.cpp b/challenge-080/ash/cpp/ch-1.cpp new file mode 100644 index 0000000000..e006218f32 --- /dev/null +++ b/challenge-080/ash/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +/* + Task 1 from + https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +*/ + +#include +#include +#include + +using namespace std; + +int missing_n(vector n) { + map v; + + int max = n[0]; + for (auto x : n) { + v[x] = true; + if (x > max) max = x; + } + + for (auto c = 1; c <= max ; c++) { + if (!v[c]) { + return c; + break; + } + } + + return max + 1; +} + +int main() { + cout << (missing_n(vector {5, 2, -2, 0}) == 1) << endl; + cout << (missing_n(vector {1, 8, -1}) == 2) << endl; + cout << (missing_n(vector {5, 2, -2, 0}) == 1) << endl; + + cout << (missing_n(vector {5, 2, 3, 1, -2, 0}) == 4) << endl; + cout << (missing_n(vector {5, 2, 3, 1, 4, -2, 0}) == 6) << endl; +} -- cgit From f3610ba736a27f0c3373bf65b424d3cbba85d2df Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Wed, 30 Sep 2020 14:43:40 +0200 Subject: Tiny optimization --- challenge-080/ash/cpp/ch-1.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-080/ash/cpp/ch-1.cpp') diff --git a/challenge-080/ash/cpp/ch-1.cpp b/challenge-080/ash/cpp/ch-1.cpp index e006218f32..278833993b 100644 --- a/challenge-080/ash/cpp/ch-1.cpp +++ b/challenge-080/ash/cpp/ch-1.cpp @@ -18,7 +18,7 @@ int missing_n(vector n) { if (x > max) max = x; } - for (auto c = 1; c <= max ; c++) { + for (auto c = 1; c < max; c++) { if (!v[c]) { return c; break; -- cgit