From 6c6480baed8abb849ed2d2ffa29f5e8710b64268 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 2 May 2023 19:35:38 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-215/ulrich-rieke/cpp/ch-2.cpp | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 challenge-215/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-215/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-215/ulrich-rieke/cpp/ch-2.cpp b/challenge-215/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..e6f54d4baa --- /dev/null +++ b/challenge-215/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector 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 << "Please enter a line of 1's and 0's, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector numberstrings( split( line, " " ) ) ; + std::vector numbers ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + std::cout << "How many 0's should be replaced ?\n" ; + int num ; + std::cin >> num ; + int necessary = 0 ; + if ( num == 1 ) + necessary = 3 ; + if ( num > 1 ) + necessary = 3 + num ; + int len = numbers.size( ) ; + if ( len < necessary || len == necessary ) + std::cout << 0 << std::endl ; + else { + std::vector results ; + for ( int start = 0 ; start < len - necessary + 1 ; start++ ) { + if ( std::all_of( numbers.begin( ) + start , numbers.begin( ) + start + + necessary , []( int n ) { return n == 0 ; } ) ) { + results.push_back( true ) ; + } + else + results.push_back( false ) ; + } + if ( std::any_of( results.begin( ) , results.end( ) , []( auto b ) { + return b == true ; })) { + std::cout << 1 << std::endl ; + } + else { + std::cout << 0 << std::endl ; + } + } + return 0 ; +} -- cgit