aboutsummaryrefslogtreecommitdiff
path: root/challenge-114/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-114/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-114/ulrich-rieke/cpp/ch-2.cpp28
1 files changed, 28 insertions, 0 deletions
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 <string>
+#include <iostream>
+#include <cstdlib>
+#include <algorithm>
+
+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 ;
+}