diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-01-09 20:41:16 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-01-09 20:41:16 +0000 |
| commit | b7282d1ea44b6251e397269c01c7689779edc05b (patch) | |
| tree | 6f5e3bbbeda711b522e6b5e0378628f581e6241d /challenge-251/ulrich-rieke/cpp/ch-1.cpp | |
| parent | ddb204a0859b471f63c44be0179af963add50e42 (diff) | |
| download | perlweeklychallenge-club-b7282d1ea44b6251e397269c01c7689779edc05b.tar.gz perlweeklychallenge-club-b7282d1ea44b6251e397269c01c7689779edc05b.tar.bz2 perlweeklychallenge-club-b7282d1ea44b6251e397269c01c7689779edc05b.zip | |
- Added solutions by Robbie Hatley.
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-251/ulrich-rieke/cpp/ch-1.cpp')
| -rwxr-xr-x | challenge-251/ulrich-rieke/cpp/ch-1.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
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 <iostream>
+#include <list>
+#include <string>
+
+std::list<std::string> split( const std::string & startline ,
+ const std::string & sep ) {
+ std::list<std::string> 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<std::string> 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 ;
+}
|
