AcWing 1537. 递归实现排列类型枚举 II
原题链接
简单
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10;
int n, q[N];
int path[N];
bool st[N];
void dfs(int u)
{
if (u == n)
{
for (int i = 0; i < n; i ++) cout << path[i] << ' ';
cout << endl;
return;
}
for (int i = 0; i < n; i ++)
{
if (st[i]) continue;
if ((i > 0) && !st[i - 1] && q[i] == q[i - 1]) continue;
st[i] = true;
path[u] = q[i];
dfs(u + 1);
st[i] = false;
path[u] -= q[i];
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++) cin >> q[i];
sort(q, q + n);
dfs(0);
return 0;
}