From 4a6109910c67852630b4d5b50d2a1bf465cfbeaa Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Sun, 20 Nov 2022 10:46:28 +0330 Subject: Challenge191 --- challenge-191/deadmarshal/cpp/ch-2.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-191/deadmarshal/cpp/ch-2.cpp (limited to 'challenge-191/deadmarshal/cpp/ch-2.cpp') 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 +#include + +bool is_cute(const std::vector& 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& 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 vec{1,2}; + int count{}; + permute(vec,0,2,&count); + std::cout << count << '\n'; + return 0; +} -- cgit