From b7c6ba230d95ffad246f0f9874e82cbd95e12f56 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 1 Aug 2023 00:30:26 +0100 Subject: - Added solutions by Niels van Dijke. - Added solutions by Ulrich Rieke. - Added solutions by Robert DiCicco. - Added solutions by Laurent Rosenfeld. - Added solutions by Peter Meszaros. - Added solutions by Mark Anderson. - Added solutions by Lubos Kolouch. - Added solutions by Ali Moradi. - Added solutions by Dave Jacoby. - Added solutions by Peter Campbell Smith. - Added solutions by W. Luis Mochan. - Added solutions by Steven Wilson. - Added solutions by Thomas Kohler. - Added solutions by E. Choroba. - Added solutions by Bob Lied. --- challenge-228/ulrich-rieke/cpp/ch-2.cpp | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 challenge-228/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-228/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-228/ulrich-rieke/cpp/ch-2.cpp b/challenge-228/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8b228dff22 --- /dev/null +++ b/challenge-228/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,45 @@ +#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 ; +} + +int main( ) { + std::cout << "Enter some unique integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector numberstrings ( split ( line , " " ) ) ; + std::list numbers ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + int rounds = 0 ; + while ( numbers.size( ) > 0 ) { + //look for the smallest element + int mini = *std::min_element( numbers.begin( ) , numbers.end( ) ) ; + auto pos = std::find( numbers.begin( ) , numbers.end( ) , mini ) ; + if ( pos == numbers.begin( ) ) //if the smallest element is at the start + //of list numbers + numbers.remove( mini ) ; //remove it + else { + int first = *numbers.begin( ) ; //save the first element + numbers.pop_front( ) ; + numbers.push_back( first ) ; //and push it at the end of the vector + } + rounds++ ; + } + std::cout << rounds << std::endl ; + return 0 ; +} -- cgit