diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-09-15 11:18:49 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-09-15 11:18:49 +0100 |
| commit | b2cc80b5507fc4f13b2eec8050f70ef12019ffb6 (patch) | |
| tree | 6cf6027c95fec4c30d7ea74b5397510787f606bc /challenge-339/ulrich-rieke/cpp/ch-2.cpp | |
| parent | b7dffbfb668d8ae3c7e9f77ef3c7da40d721da11 (diff) | |
| download | perlweeklychallenge-club-b2cc80b5507fc4f13b2eec8050f70ef12019ffb6.tar.gz perlweeklychallenge-club-b2cc80b5507fc4f13b2eec8050f70ef12019ffb6.tar.bz2 perlweeklychallenge-club-b2cc80b5507fc4f13b2eec8050f70ef12019ffb6.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-339/ulrich-rieke/cpp/ch-2.cpp')
| -rwxr-xr-x | challenge-339/ulrich-rieke/cpp/ch-2.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-339/ulrich-rieke/cpp/ch-2.cpp b/challenge-339/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..45bf047704 --- /dev/null +++ b/challenge-339/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,35 @@ +#include <vector>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+#include <string>
+
+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 height differences separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> differences , heights ;
+ for ( auto s : tokens ) {
+ differences.push_back( std::stoi( s ) ) ;
+ }
+ heights.push_back( 0 ) ;
+ int currentHeight = 0 ;
+ for ( auto it = differences.begin( ) ; it != differences.end( ) ; ++it ) {
+ currentHeight += *it ;
+ heights.push_back( currentHeight ) ;
+ }
+ std::cout << *std::max_element( heights.begin( ) , heights.end( ) ) <<
+ '\n' ;
+ return 0 ;
+}
+
|
