diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2022-11-20 10:46:28 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2022-11-20 10:46:28 +0330 |
| commit | 4a6109910c67852630b4d5b50d2a1bf465cfbeaa (patch) | |
| tree | 6e42048916f594d7761bf110717780a31e556b05 /challenge-191/deadmarshal/cpp | |
| parent | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff) | |
| download | perlweeklychallenge-club-4a6109910c67852630b4d5b50d2a1bf465cfbeaa.tar.gz perlweeklychallenge-club-4a6109910c67852630b4d5b50d2a1bf465cfbeaa.tar.bz2 perlweeklychallenge-club-4a6109910c67852630b4d5b50d2a1bf465cfbeaa.zip | |
Challenge191
Diffstat (limited to 'challenge-191/deadmarshal/cpp')
| -rw-r--r-- | challenge-191/deadmarshal/cpp/ch-1.cpp | 23 | ||||
| -rw-r--r-- | challenge-191/deadmarshal/cpp/ch-2.cpp | 34 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-191/deadmarshal/cpp/ch-1.cpp b/challenge-191/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..324018050b --- /dev/null +++ b/challenge-191/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,23 @@ +#include<cstdio> +#include<vector> +#include<algorithm> + +int twice_largest(std::vector<int>& vec) +{ + std::sort(vec.begin(), vec.end(), std::greater<int>()); + return vec[0] >= (2 * vec[1]) ? 1 : -1; +} + +int main() +{ + std::vector<int> vec1{1,2,3,4}; + std::vector<int> vec2{1,2,0,5}; + std::vector<int> vec3{2,6,3,1}; + std::vector<int> vec4{4,5,2,3}; + printf("%2d\n", twice_largest(vec1)); + printf("%2d\n", twice_largest(vec2)); + printf("%2d\n", twice_largest(vec3)); + printf("%2d\n", twice_largest(vec4)); + return 0; +} + diff --git a/challenge-191/deadmarshal/cpp/ch-2.cpp b/challenge-191/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..9af54816b1 --- /dev/null +++ b/challenge-191/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,34 @@ +#include<iostream> +#include<vector> + +bool is_cute(const std::vector<int>& vec) +{ + for(std::size_t i = 1; i <= vec.size(); ++i) + if((i % vec[i-1]) && (vec[i-1] % i)) return false; + return true; +} + +void permute(std::vector<int>& vec, int i, std::size_t sz, int *count) +{ + if(sz == i) + { + if(is_cute(vec)) (*count)++; + return; + } + for(std::size_t j = i; j < sz; ++j) + { + std::swap(vec[i], vec[j]); + permute(vec,i+1,sz,count); + std::swap(vec[i], vec[j]); + } + return; +} + +int main() +{ + std::vector<int> vec{1,2}; + int count{}; + permute(vec,0,2,&count); + std::cout << count << '\n'; + return 0; +} |
