You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
566 B

#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n;
int a[N], al;
bool st[N];
void dfs(int u) {
// 如果到达了终点
if (u == n + 1) {
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
puts("");
return;
}
for (int i = 1; i <= n; i++)
if (!st[i]) {
a[al++] = i;
st[i] = true;
dfs(u + 1);
st[i] = false;
al--;
}
}
int main() {
cin >> n;
// 开始
dfs(1);
return 0;
}