From 5490af6ff65f608b9a8d4933e86f67627650de27 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 7 Feb 2020 10:17:46 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-046/ulrich-rieke/cpp/ch-2.cpp | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-046/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-046/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-046/ulrich-rieke/cpp/ch-2.cpp b/challenge-046/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..a363ff9209 --- /dev/null +++ b/challenge-046/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +std::string flip( const std::string & doorstate ) { + if ( doorstate == "open" ) { + return "closed" ; + } + else { + return "open" ; + } +} + +int main( ) { + std::vector > theDoors ; + for ( int i = 1 ; i < 501 ; i++ ) { + theDoors.push_back( std::make_pair( "open" , i ) ) ; + } + for ( int i = 2 ; i < 501 ; i++ ) { + if ( i < 251 ) { + for ( int j = i ; j < 501 ; j += i ) { + const std::string state {theDoors[ j - 1].first } ; + std::string newState { flip ( state ) } ; + theDoors[ j - 1 ] = std::make_pair( newState , theDoors[ j - 1 ].second ) ; + } + } + else { + theDoors[ i - 1 ] = std::make_pair( flip( theDoors[ i - 1 ].first ) , + theDoors[ i - 1 ].second ) ; + } + } + for ( auto & p : theDoors ) { + if ( p.first == "open" ) { + std::cout << "Door " << p.second << " is open!\n" ; + } + } + return 0 ; +} -- cgit