diff options
Diffstat (limited to 'challenge-262/deadmarshal/cpp')
| -rw-r--r-- | challenge-262/deadmarshal/cpp/ch-1.cpp | 21 | ||||
| -rw-r--r-- | challenge-262/deadmarshal/cpp/ch-2.cpp | 21 |
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-262/deadmarshal/cpp/ch-1.cpp b/challenge-262/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..391ea1f286 --- /dev/null +++ b/challenge-262/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,21 @@ +#include<iostream> +#include<vector> + +template<typename T> +std::size_t max_positive_negative(const std::vector<T> &vec) +{ + std::size_t neg{},pos{}; + for(const T& e : vec) if(e < 0) neg++; else pos++; + return static_cast<size_t>(std::max(neg,pos)); +} + +int main() +{ + std::vector<int> vec1{-3,1,2,-1,3,-2,4}, + vec2{-1,-2,-3,1},vec3{1,2}; + std::cout << max_positive_negative<int>(vec1) << '\n' + << max_positive_negative<int>(vec2) << '\n' + << max_positive_negative<int>(vec3) << '\n'; + return 0; +} + diff --git a/challenge-262/deadmarshal/cpp/ch-2.cpp b/challenge-262/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..b5da7bc33c --- /dev/null +++ b/challenge-262/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,21 @@ +#include<iostream> +#include<vector> + +template<typename T> +std::size_t count_equal_divisible(const std::vector<T> &vec,int k) +{ + size_t count{}; + for(size_t i = 0; i < vec.size()-1; ++i) + for(size_t j = i+1; j < vec.size(); ++j) + if((vec.at(i) == vec.at(j)) && ((i*j) % k == 0)) count++; + return count; +} + +int main() +{ + std::vector<int> vec1{3,1,2,2,2,1,3},vec2{1,2,3}; + std::cout << count_equal_divisible<int>(vec1,2) << '\n' + << count_equal_divisible<int>(vec2,1) << '\n'; + return 0; +} + |
