binary n bit, k bit is "on" state

not my code
 avatar
ManhDX
c_cpp
2 years ago
952 B
2
Indexable
Never
#include<bits/stdc++.h>

using namespace std;

int n, k, check;
vector<vector<int>> res;
vector<int> a;

void khoi_tao(vector<int> &x)
{
    for ( int i=0 ; i<n ; i++ )
    {
        x.push_back(0);
    }
}

bool check1()
{
    int cnt=0;

    for (int i=0 ; i<=n-1 ; i++ )
    {
        if ( a[i]==1 )
            cnt++;
    }
    if (cnt==k)
        return true;
    else
        return false;
}

void sinh()
{
    int i=n-1;
    while (i>=0 && a[i]==1)
    {
        a[i]=0;
        --i;
    }
    if (i==-1) 
    {
        check=0;
    }
    else
    {
        a[i]=1;
    }
    check1();
}


int main()
{
    cin>>n>>k;
    khoi_tao(a);
    check=1;
    while (check)
    {
        if (check1()==true) res.push_back(a);
        sinh();
    }
    for (vector<int> y : res)
    {
        for (int x : y)
        {
            cout << x;
        }   
        cout << endl;
    } 
}