diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-10-04 22:29:28 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-10-04 22:29:28 +0100 |
| commit | f6f0a4306d8843f5732536a95f3c61d8f3c2e300 (patch) | |
| tree | 14ea760ae27b7a9706c637ffd3ab96c58e4c3612 /challenge-080/adam-russell/cpp | |
| parent | c6d2624274ad79aa421c79853b888b6785f22ef1 (diff) | |
| download | perlweeklychallenge-club-f6f0a4306d8843f5732536a95f3c61d8f3c2e300.tar.gz perlweeklychallenge-club-f6f0a4306d8843f5732536a95f3c61d8f3c2e300.tar.bz2 perlweeklychallenge-club-f6f0a4306d8843f5732536a95f3c61d8f3c2e300.zip | |
- Added solutions by Adam Russell.
Diffstat (limited to 'challenge-080/adam-russell/cpp')
| -rw-r--r-- | challenge-080/adam-russell/cpp/ch-1.cpp | 25 | ||||
| -rw-r--r-- | challenge-080/adam-russell/cpp/ch-2.cpp | 30 |
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-080/adam-russell/cpp/ch-1.cpp b/challenge-080/adam-russell/cpp/ch-1.cpp new file mode 100644 index 0000000000..c13f1c8c43 --- /dev/null +++ b/challenge-080/adam-russell/cpp/ch-1.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <algorithm> +#include <vector> +/* +* You are given an unsorted list of integers @N. +* Write a script to find out the smallest positive number missing. +*/ +int least_missing(int n[]){ + std::sort (n, n + 4); + std::vector<int> numbers (n, n + 4); + for(int i = n[0]; i < n[3]; i++){ + std::vector<int>::iterator itr = std::find(numbers.begin(), numbers.end(), i); + if (itr == std::end(numbers)) { + if(i > 0) + return i; + } + } + return -1; +} + +int main(int argc, char** argv){ + int N[4] = {5, 2, -2, 0}; + int i = least_missing(N); + std::cout << "the least positive missing number is " << i << std::endl; +}
\ No newline at end of file diff --git a/challenge-080/adam-russell/cpp/ch-2.cpp b/challenge-080/adam-russell/cpp/ch-2.cpp new file mode 100644 index 0000000000..f3e26491c9 --- /dev/null +++ b/challenge-080/adam-russell/cpp/ch-2.cpp @@ -0,0 +1,30 @@ +#include <iostream> +/* +* You are given rankings of @N candidates. +* Write a script to find out the total candies needed for all candidates. +* You are asked to follow the rules below: +* a) You must given at least one candy to each candidate. +* b) Candidate with higher ranking get more candies than their immediate +* neighbors on either side. +*/ +int count_candies(int candidates[]){ + int candies = 4; + for(int i = 0; i < 3; i++){ + if((i - 1) >= 0){ + if(candidates[i] > candidates[i - 1]){ + candies++; + } + } + if((i + 1) < 4){ + if(candidates[i] > candidates[i + 1]){ + candies++; + } + } + } + return candies; +} +int main(int argc, char** argv){ + int N[4] = {1, 4, 3, 2}; + int i = count_candies(N); + std::cout << "the number of candies is " << i << std::endl; +}
\ No newline at end of file |
