From e2bcd5b91f30ead0274fca78de01038b4432995b Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 12 Jun 2023 05:17:44 +0100 Subject: - 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. --- challenge-220/ulrich-rieke/cpp/ch-2.cpp | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 challenge-220/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-220/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector 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 & subarray ) { + for ( int i = 0 ; i < subarray.size( ) - 1 ; i++ ) { + int sum = subarray[ i ] + subarray[ i + 1 ] ; + double root = std::sqrt( static_cast(sum) ) ; + int rootint = static_cast(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 numberstrings ( split( line , " " ) ) ; + std::vector numbers ; + for ( auto s : numberstrings ) { + numbers.push_back( std::stoi( s ) ) ; + } + std::sort( numbers.begin( ) , numbers.end( ) ) ; + std::vector> 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 ; +} -- cgit