From b2cc80b5507fc4f13b2eec8050f70ef12019ffb6 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 15 Sep 2025 11:18:49 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-339/ulrich-rieke/cpp/ch-2.cpp | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 challenge-339/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-339/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include +#include +#include + +std::vector split( const std::string & text , char delimiter ) { + std::vector 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 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 ; +} + -- cgit