Skip to content

Instantly share code, notes, and snippets.

@magiskboy
Last active April 29, 2021 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magiskboy/25235d1e013c0452905d90e281d44f1c to your computer and use it in GitHub Desktop.
Save magiskboy/25235d1e013c0452905d90e281d44f1c to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
int v[100];
int n;
void gen(int i) {
if (i == n) {
for (int j = 0; j < n; ++j) cout << v[j] << " ";
cout << "\n";
return;
}
for (int j = 0; j < 2; ++j) {
v[i] = j;
gen(i+1);
}
}
int main() {
n = 3;
gen(0);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int n;
void gen(int s) {
for (int i = 1; i < n + 1; ++i) {
if (s < i) {
for (auto x : v) cout << x << " ";
cout << s << "\n";
return;
}
if (s > i) {
v.push_back(i);
gen(s-i);
v.pop_back();
}
}
}
int main() {
cin >> n;
gen(n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment