Pagini recente » Cod sursa (job #829532) | Cod sursa (job #3354947)
#include <bits/stdc++.h>
using namespace std;
ofstream out("combinari.out");
int n, k;
set<int> numbers;
void bkt(int n, int k, vector<int> path) {
if ((int)path.size() == k) {
for (int x : path) {
out << x << " ";
}
out << "\n";
return;
}
for (int i = 1; i <= n; i++) {
if (numbers.find(i) == numbers.end()) {
if (!path.empty() && path.back() < i) {
path.push_back(i);
numbers.insert(i);
bkt(n, k, path);
path.pop_back();
numbers.erase(i);
}
else if (path.empty()) {
path.push_back(i);
numbers.insert(i);
bkt(n, k, path);
path.pop_back();
numbers.erase(i);
}
}
}
}
int main() {
ifstream f("combinari.in");
f >> n >> k;
vector<int> path;
bkt(n, k, path);
return 0;
}