aboutsummaryrefslogtreecommitdiff
path: root/challenge-220/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-06-12 05:17:44 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-06-12 05:17:44 +0100
commite2bcd5b91f30ead0274fca78de01038b4432995b (patch)
treead9068d3c9efe097349ab136ebac3d16aaadbaa8 /challenge-220/ulrich-rieke/cpp/ch-2.cpp
parentb69ae3ee9ee4535d66c3c1cb5c4b8fb3712dd75c (diff)
downloadperlweeklychallenge-club-e2bcd5b91f30ead0274fca78de01038b4432995b.tar.gz
perlweeklychallenge-club-e2bcd5b91f30ead0274fca78de01038b4432995b.tar.bz2
perlweeklychallenge-club-e2bcd5b91f30ead0274fca78de01038b4432995b.zip
- Added solutions by Roger Bell_West.
- Added solutions by Robert DiCicco. - Added solutions by Ulrich Rieke. - Added solutions by Laurent Rosenfeld. - Added solutions by Niels van Dijke. - Added solutions by Simon Proctor. - Added solutions by Mark Anderson. - Added solutions by Peter Meszaros. - Added solutions by W. Luis Mochan. - Added solutions by David Ferrone. - Added solutions by Thomas Kohler. - Added solutions by Stephen G. Lynn. - Added solutions by Peter Campbell Smith. - Added solutions by E. Choroba. - Added solutions by Robbie Hatley. - Added solutions by Jorg Sommrey. - Added solutions by Cheok-Yin Fung. - Added solutions by Robert Ransbottom. - Added solutions by Flavio Poletti. - Added solutions by Jaldhar H. Vyas. - Added solutions by Avery Adams. - Added solutions by Bob Lied. - Added solutions by Athanasius. - Added solutions by Simon Green. - Added solutions by Jan Krnavek. - Added solutions by Lubos Kolouch. - Added solutions by BarrOff. - Added solutions by Solathian. - Added solutions by Matthias Muth.
Diffstat (limited to 'challenge-220/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-220/ulrich-rieke/cpp/ch-2.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/challenge-220/ulrich-rieke/cpp/ch-2.cpp b/challenge-220/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..d8dac8ff9f
--- /dev/null
+++ b/challenge-220/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,69 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+#include <cmath>
+
+std::vector<std::string> split( const std::string & startline ,
+ const std::string & sep ) {
+ std::vector<std::string> separated ;
+ std::string::size_type start { 0 } ;
+ std::string::size_type pos ;
+ do {
+ pos = startline.find_first_of( sep , start ) ;
+ separated.push_back( startline.substr(start , pos - start )) ;
+ start = pos + 1 ;
+ } while ( pos != std::string::npos ) ;
+ return separated ;
+}
+
+bool condition( const std::vector<int> & subarray ) {
+ for ( int i = 0 ; i < subarray.size( ) - 1 ; i++ ) {
+ int sum = subarray[ i ] + subarray[ i + 1 ] ;
+ double root = std::sqrt( static_cast<double>(sum) ) ;
+ int rootint = static_cast<int>(std::floor( root ) ) ;
+ if ( rootint * rootint != sum ) {
+ return false ;
+ }
+ }
+ return true ;
+}
+
+int main( ) {
+ std::cout << "Please enter some integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings ( split( line , " " ) ) ;
+ std::vector<int> numbers ;
+ for ( auto s : numberstrings ) {
+ numbers.push_back( std::stoi( s ) ) ;
+ }
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ std::vector<std::vector<int>> solution ;
+ if ( condition( numbers ) ) {
+ solution.push_back( numbers ) ;
+ }
+ while ( std::next_permutation( numbers.begin( ) , numbers.end( ) ) ) {
+ if ( condition ( numbers ) ) {
+ solution.push_back( numbers ) ;
+ }
+ }
+ std::cout << "(" ;
+ if ( solution.size( ) == 0 ) {
+ std::cout << "())\n" ;
+ }
+ else {
+ for ( auto seq : solution ) {
+ std::cout << "(" ;
+ for ( int i : seq ) {
+ std::cout << i << " ";
+ }
+ std::cout << ")" ;
+ if ( seq != solution.back( ) ) {
+ std::cout << "," ;
+ }
+ }
+ std::cout << ")\n" ;
+ }
+ return 0 ;
+}