From b7282d1ea44b6251e397269c01c7689779edc05b Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 9 Jan 2024 20:41:16 +0000 Subject: - Added solutions by Robbie Hatley. - Added solutions by Ulrich Rieke. --- challenge-251/ulrich-rieke/cpp/ch-1.cpp | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 challenge-251/ulrich-rieke/cpp/ch-1.cpp (limited to 'challenge-251/ulrich-rieke/cpp/ch-1.cpp') diff --git a/challenge-251/ulrich-rieke/cpp/ch-1.cpp b/challenge-251/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..75c7efe2e4 --- /dev/null +++ b/challenge-251/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +std::list split( const std::string & startline , + const std::string & sep ) { + std::list 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 integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::list numberlist { split( line , " " ) } ; + int sum = 0 ; + while ( numberlist.size( ) > 0 ) { + if ( numberlist.size( ) > 1 ) { + std::string concatenated { numberlist.front( ).append( + numberlist.back( )) } ; + sum += std::stoi( concatenated ) ; + numberlist.pop_front( ) ; + numberlist.pop_back( ) ; + } + else { + sum += std::stoi( numberlist.front( ) ) ; + numberlist.pop_front( ) ; + } + } + std::cout << sum << '\n' ; + return 0 ; +} -- cgit