fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // Function to find all N-digit binary numbers having
  5. // more 1's than 0's at any position
  6. void Solution(std::string _currentNumber, int _extraOnes, int _remainingPlaces)
  7. {
  8. // If the number is completed, print it
  9. if (0 == _remainingPlaces)
  10. {
  11. std::cout << _currentNumber << std::endl;
  12. return;
  13. }
  14.  
  15. // Append 1 to the current number and reduce the remaining places by one
  16. Solution(_currentNumber + "1", _extraOnes + 1, _remainingPlaces - 1);
  17.  
  18. // If there are more ones than zeroes, append 0 to the current number
  19. // and reduce the remaining places by one
  20. if (0 < _extraOnes)
  21. {
  22. Solution(_currentNumber + "0", _extraOnes - 1, _remainingPlaces - 1);
  23. }
  24. }
  25.  
  26. int main(void)
  27. {
  28. const int numberOfDigits = 4;
  29. std::string str;
  30.  
  31. Solution(str, 0, numberOfDigits);
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
1111
1110
1101
1100
1011
1010