aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/deadmarshal/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-21 04:57:01 +0000
committerGitHub <noreply@github.com>2022-11-21 04:57:01 +0000
commit054a1d856f6600be605d149f7830a7426f828fce (patch)
treeee38a9bf6eef686793ab957d964d2c70cb4d3ee7 /challenge-191/deadmarshal/cpp/ch-2.cpp
parent7ac92a4a7fd76a307feefe75fcb666f9525e1b7b (diff)
parent4a6109910c67852630b4d5b50d2a1bf465cfbeaa (diff)
downloadperlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.tar.gz
perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.tar.bz2
perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.zip
Merge pull request #7110 from deadmarshal/challenge191
Challenge191
Diffstat (limited to 'challenge-191/deadmarshal/cpp/ch-2.cpp')
-rw-r--r--challenge-191/deadmarshal/cpp/ch-2.cpp34
1 files changed, 34 insertions, 0 deletions
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;
+}