Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created August 31, 2019 06:20
Show Gist options
  • Save surinoel/fec1c31968db14509e301764194e3fd6 to your computer and use it in GitHub Desktop.
Save surinoel/fec1c31968db14509e301764194e3fd6 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
bool visit[9];
vector<int> num;
vector<vector<int>> ans;
void go(int maxcnt, vector<int> v) {
if (maxcnt == m) {
ans.push_back(v);
return;
}
for (int i = 0; i < n; i++) {
if (!visit[i]) {
visit[i] = true;
v.push_back(num[i]);
go(maxcnt + 1, v);
v.pop_back();
visit[i] = false;
}
}
return;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
num.resize(n);
for (int i = 0; i < n; i++) {
cin >> num[i];
}
sort(num.begin(), num.end());
go(0, vector<int>());
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment