Answer the question
In order to leave comments, you need to log in
Binary vector permutations
Good evening, I need to output all permutations of a vector of a certain length and a certain weight. Here is how I implemented.
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
vector<int> generator(int n, int m) {
vector <int> v;
for(int i=0; i<pow(2,n)-1; i++ ) {
if(i<=m-1) v.push_back(1);
else v.push_back(0);
}
return v;
}
int main()
{
int n,m;
cout<<"N:";
cin>>n;
cout<<"M:";
cin>>m;
vector<int> v = generator(n,m);
do
{
for(int i=0; i<v.size(); i++)
cout <<v[i]<<" ";
cout<<endl;
}
while(next_permutation(v.begin(), v.end()));
return 0;
}
Answer the question
In order to leave comments, you need to log in
Pushkin will sort?)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
vector<int> generator(int n, int m) {
vector <int> v;
for(int i=0; i<pow((double)2,n)-1; i++ ) {
if(i<=m-1) v.push_back(1);
else v.push_back(0);
}
return v;
}
int main()
{
int n,m;
cout<<"N:";
cin>>n;
cout<<"M:";
cin>>m;
vector<int> v = generator(n,m);
sort(v.begin(), v.end());
do
{
for(int i=0; i<v.size(); i++)
cout <<v[i]<<" ";
cout<<endl;
}
while(next_permutation(v.begin(), v.end()));
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question