Advertisement
Scramper

all bitsets with n digits and k ones

Nov 22nd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <bitset>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. vector<int> input() {
  11.   string s;
  12.   getline(std::cin, s);
  13.   std::vector<int> result;
  14.   std::istringstream iss(s);
  15.  
  16.   for (string s; iss >> s;) {
  17.     result.push_back(stol(s));
  18.   }
  19.   return(result);
  20. }
  21.  
  22. void print_binary(size_t stringLength, size_t numberOfOnes) {
  23.   unsigned long long int maxNum = pow(2, stringLength);
  24.   unsigned long long int minNum = pow(2, numberOfOnes);
  25.   cout << maxNum << " " << minNum << endl;
  26.   for (unsigned long long int digit = minNum - 1; digit < maxNum; ++digit) {
  27.     std::bitset<100> fooBitset(digit);
  28.     if (fooBitset.count() == numberOfOnes) {
  29.         string foo = fooBitset.to_string<char, std::string::traits_type>();
  30.         cout << foo.substr(100 - stringLength, 100) << endl;
  31.     }
  32.   }
  33. }
  34.  
  35. int main() {
  36.   std::vector<int> parameters = input();
  37.   print_binary(parameters[0], parameters[1]);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement