diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-08-01 00:30:26 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-08-01 00:30:26 +0100 |
| commit | b7c6ba230d95ffad246f0f9874e82cbd95e12f56 (patch) | |
| tree | c4ba4a2568676aba47a13dba8dcd5ea206366461 /challenge-228/ulrich-rieke/cpp/ch-2.cpp | |
| parent | b7b0353c40a650fce3847d239aa83c2eb67086ac (diff) | |
| download | perlweeklychallenge-club-b7c6ba230d95ffad246f0f9874e82cbd95e12f56.tar.gz perlweeklychallenge-club-b7c6ba230d95ffad246f0f9874e82cbd95e12f56.tar.bz2 perlweeklychallenge-club-b7c6ba230d95ffad246f0f9874e82cbd95e12f56.zip | |
- 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.
Diffstat (limited to 'challenge-228/ulrich-rieke/cpp/ch-2.cpp')
| -rwxr-xr-x | challenge-228/ulrich-rieke/cpp/ch-2.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
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 <iostream>
+#include <string>
+#include <list>
+#include <vector>
+#include <algorithm>
+
+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 ;
+}
+
+int main( ) {
+ std::cout << "Enter some unique integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings ( split ( line , " " ) ) ;
+ std::list<int> 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 ;
+}
|
