From 5ea37ecb6cdebf9514cb102455e31beeebcafc69 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 25 May 2021 21:18:39 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-114/ulrich-rieke/cpp/ch-2.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-114/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-114/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-114/ulrich-rieke/cpp/ch-2.cpp b/challenge-114/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..6f0b4fcb4d --- /dev/null +++ b/challenge-114/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +std::string toBinaryString( int n ) { + std::string binaryString ; + while ( n != 0 ) { + int i = n % 2 ; + binaryString.append( std::to_string( i )) ; + n /= 2 ; + } + reverse( binaryString.begin( ) , binaryString.end( ) ) ; + return binaryString ; +} + +int main( int argc , char * argv[] ) { + int n = std::atoi( argv[ 1 ] ) ; + std::string numberstring( toBinaryString( n ) ) ; + int ones = std::count( numberstring.begin( ) , numberstring.end( ) , '1' ) ; + numberstring = toBinaryString( ++n ) ; + while ( std::count( numberstring.begin( ) , numberstring.end( ) , '1' ) != ones ) { + n++ ; + numberstring = toBinaryString( n ) ; + } + std::cout << n << std::endl ; + return 0 ; +} -- cgit