Pagini recente » Cod sursa (job #1223924) | Cod sursa (job #60406) | Cod sursa (job #1301309) | Cod sursa (job #3269254) | Cod sursa (job #3354952)
#include <bits/stdc++.h>
using namespace std;
ofstream out("combinari.out");
int n, k;
set<int> used; // <-- redenumit din 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 (used.find(i) == used.end()) {
if (!path.empty() && path.back() < i) {
path.push_back(i);
used.insert(i);
bkt(n, k, path);
path.pop_back();
used.erase(i);
}
else if (path.empty()) {
path.push_back(i);
used.insert(i);
bkt(n, k, path);
path.pop_back();
used.erase(i);
}
}
}
}
int main() {
ifstream f("combinari.in");
f >> n >> k;
vector<int> path;
bkt(n, k, path);
return 0;
}