diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-12-20 14:06:10 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-12-20 14:06:10 +0000 |
| commit | d05040719c41d928cb4663bac89448f14fd74268 (patch) | |
| tree | daf65dce885ce52d87542dcfd6dc1c687608ccb9 /challenge-196/ulrich-rieke/cpp/ch-2.cpp | |
| parent | b68fe4b8bfc540612fb0f86d94ae39162039081e (diff) | |
| download | perlweeklychallenge-club-d05040719c41d928cb4663bac89448f14fd74268.tar.gz perlweeklychallenge-club-d05040719c41d928cb4663bac89448f14fd74268.tar.bz2 perlweeklychallenge-club-d05040719c41d928cb4663bac89448f14fd74268.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-196/ulrich-rieke/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-196/ulrich-rieke/cpp/ch-2.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-196/ulrich-rieke/cpp/ch-2.cpp b/challenge-196/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..8545530a9a --- /dev/null +++ b/challenge-196/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,65 @@ +#include <iostream> +#include <vector> +#include <string> + +std::vector<std::string> split( const std::string & startline , const std::string & sep ) { + std::vector<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 findEnd( const std::vector<int> & array , int pos ) { + int current_index = pos ; + current_index++ ; + if ( current_index > array.size( ) - 1 ) { + return pos ; + } + else { + int current_number = array[ current_index ] ; + while ( current_number - array[ current_index - 1 ] == 1 ) { + current_index++ ; + if ( current_index < array.size( ) ) + current_number = array[ current_index ] ; + else + break ; + } + current_index-- ; + return current_index ; + } +} + +int main( ) { + std::cout << "Please enter a sorted array of unique integers!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings( split( line , " " ) ) ; + std::vector<int> numbers ; + for ( auto & s : numberstrings ) { + numbers.push_back( std::stoi( s ) ) ; + } + std::vector<int> subarray ; + std::vector<std::vector<int>> continuousRuns ; + int pos = 0 ; + while ( pos < numbers.size( ) ) { + int end = findEnd( numbers , pos ) ; + if ( end - pos > 0 ) { + subarray.push_back( numbers[ pos ] ) ; + subarray.push_back( numbers[ end ] ) ; + continuousRuns.push_back( subarray ) ; + subarray.clear( ) ; + } + pos = end + 1 ; + } + for ( const auto & suba : continuousRuns ) { + std::cout << '[' << suba[0] << ',' << suba[1] << ']' ; + std::cout << " ," ; + } + std::cout << std::endl ; + return 0 ; +} |
