aboutsummaryrefslogtreecommitdiff
path: root/challenge-321/ulrich-rieke/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-05-13 13:30:54 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-05-13 13:30:54 +0100
commita799bc96049a853dae3a0b8bf14c4eb581a4a4ba (patch)
tree129fea9bbf713c223e3ad23f6b43ef3a17397340 /challenge-321/ulrich-rieke/cpp/ch-1.cpp
parent152c0ffaaa3df8dfa2a5dbff97971ec378999b0a (diff)
downloadperlweeklychallenge-club-a799bc96049a853dae3a0b8bf14c4eb581a4a4ba.tar.gz
perlweeklychallenge-club-a799bc96049a853dae3a0b8bf14c4eb581a4a4ba.tar.bz2
perlweeklychallenge-club-a799bc96049a853dae3a0b8bf14c4eb581a4a4ba.zip
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke. - Added solutions by Mark Anderson. - Added solutions by Feng Chang. - Added solutions by E. Choroba. - Added solutions by Niels van Dijke. - Added solutions by Andreas Mahnke. - Added solutions by Luca Ferrari. - Added solutions by Andrew Shitov. - Added solutions by Ali Moradi. - Added solutions by W. Luis Mochan. - Added solutions by David Ferrone. - Added solutions by Conor Hoekstra. - Added solutions by Peter Meszaros. - Added solutions by Robert Ransbottom. - Added solutions by Peter Campbell Smith. - Added solutions by Robbie Hatley.
Diffstat (limited to 'challenge-321/ulrich-rieke/cpp/ch-1.cpp')
-rwxr-xr-xchallenge-321/ulrich-rieke/cpp/ch-1.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-321/ulrich-rieke/cpp/ch-1.cpp b/challenge-321/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..6dc8d9402b
--- /dev/null
+++ b/challenge-321/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,38 @@
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <numeric>
+#include <set>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ) )
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some numbers , their total number must be even!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<double> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stod( s ) ) ;
+ std::set<double> averages ;
+ while ( numbers.size( ) > 0 ) {
+ auto maxpos = std::max_element( numbers.begin( ) , numbers.end( ) ) ;
+ auto maxi = *maxpos ;
+ numbers.erase( maxpos ) ;
+ auto minpos = std::min_element( numbers.begin( ) , numbers.end( ) ) ;
+ auto mini = *minpos ;
+ numbers.erase( minpos ) ;
+ double average = ( maxi + mini ) / 2.0 ;
+ averages.insert( average ) ;
+ }
+ std::cout << averages.size( ) << '\n' ;
+ return 0 ;
+}