Zeros and ones (1)
Write a backtracking program to print all the combinations of z zeros and o ones such that z + o = n, for a given n.
Input: Input consists of a natural number n > 0.
Output: Print all the combinations of z zeros and o ones such that z + o = n, one per line and in lexicographical order.
Although a backtracking program is not really necessary to solve this exercise, implement it anyway for the sake of practice.
Hint
Revise the basic template for backtracking
A possible solution
// Zeros and ones (1)
#include<iostream>
#include<vector>
using namespace std;
void write (const vector<bool> & v){
<< v[0];
cout int n = v.size;
for (int i = 1; i < n; ++i)
<< " " << v[i];
cout << endl;
cout }
void backtrack(vector<bool>& sol, int k){
int n = sol.size();
if (k == n) write(sol);
else{
[k] = false;
sol(sol, k+1);
backtrack[k] = true;
sol(sol, k+1);
backtrack}
}
int main(){
int n;
>> n;
cin <bool> sol(n, false);
vector(sol, 0);
backtrack}