aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-11-14 20:13:23 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-11-14 20:13:23 +0000
commit53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a (patch)
tree211b76647896984d2aaa749e6dff28ac9d393de5 /challenge-191/ulrich-rieke/cpp/ch-2.cpp
parentbd2f6928838230547dc8de83d54a6a708f86461a (diff)
downloadperlweeklychallenge-club-53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a.tar.gz
perlweeklychallenge-club-53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a.tar.bz2
perlweeklychallenge-club-53324ed8af4d2e2211d4ce6cc51d1c0471d30c3a.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-191/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-191/ulrich-rieke/cpp/ch-2.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-191/ulrich-rieke/cpp/ch-2.cpp b/challenge-191/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..0cb6d01f46
--- /dev/null
+++ b/challenge-191/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+#include <utility>
+#include <numeric>
+
+std::vector<std::pair<int , int>> createPairs( const std::vector<int>
+ & numbers ) {
+ std::vector<std::pair<int , int>> allPairs ;
+ int count = 1 ;
+ for ( int n : numbers ) {
+ allPairs.push_back( std::make_pair( count , n )) ;
+ count++ ;
+ }
+ return allPairs ;
+}
+
+bool isCute( const std::vector<std::pair<int , int>> & allPairs ) {
+ return std::all_of( allPairs.begin( ) , allPairs.end( ) , []( const
+ auto & p) { return p.first % p.second == 0 || p.second % p.first == 0 ;}) ;
+}
+
+int main( ) {
+ std::cout << "Please enter an integer greater than 0 and up to 15 inclusive!\n" ;
+ int number ;
+ std::cin >> number ;
+ std::vector<int> numbers ( number ) ;
+ std::iota( numbers.begin( ) , numbers.end( ) , 1 ) ;
+ int count = 0 ;
+ do {
+ std::vector<std::pair<int , int>> thePairs ( createPairs( numbers )) ;
+ if ( isCute( thePairs )) {
+ count++ ;
+ }
+ } while ( std::next_permutation( numbers.begin( ) , numbers.end( ))) ;
+ std::cout << count << std::endl ;
+ return 0 ;
+}