diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-30 22:07:13 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-30 22:07:13 +0100 |
| commit | 3114956979e812e8ca166cb84d8dea346cee22d3 (patch) | |
| tree | c15e952faad2616c26483396368178c686903f6e /challenge-289/ulrich-rieke/cpp/ch-1.cpp | |
| parent | 8c1a0beec1d0dac09dd376bedb36eb11cc56dfbb (diff) | |
| download | perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.tar.gz perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.tar.bz2 perlweeklychallenge-club-3114956979e812e8ca166cb84d8dea346cee22d3.zip | |
- Added solutions by Ulrich Rieke.
- Added solutions by Paulo Custodio.
- Added solutions by David Ferrone.
- Added solutions by Thomas Kohler.
- Added solutions by Torgny Lyon.
Diffstat (limited to 'challenge-289/ulrich-rieke/cpp/ch-1.cpp')
| -rwxr-xr-x | challenge-289/ulrich-rieke/cpp/ch-1.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-289/ulrich-rieke/cpp/ch-1.cpp b/challenge-289/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..0bdc6ca2ed --- /dev/null +++ b/challenge-289/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,48 @@ +#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <set>
+#include <algorithm>
+
+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 integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto w : tokens ) {
+ numbers.push_back( std::stoi( w ) ) ;
+ }
+ if ( numbers.size( ) < 3 )
+ std::cout << *std::max_element( numbers.begin( ) , numbers.end( ) ) <<
+ '\n' ;
+ else {
+ std::set<int> uniques { numbers.begin( ) , numbers.end( ) } ;
+ std::vector<int> for_sorting { uniques.begin( ) , uniques.end( ) } ;
+ if ( for_sorting.size( ) < 3 ) {
+ std::cout << *std::max_element( for_sorting.begin( ) , for_sorting.end( ) )
+ << '\n' ;
+ }
+ else {
+ std::sort( for_sorting.begin( ) , for_sorting.end( ) , []( int a , int b) {
+ return a > b ; } ) ;
+ std::cout << *(for_sorting.begin( ) + 2 ) << '\n' ;
+ }
+ }
+ return 0 ;
+}
+
+
+
+
+
|
