#include <iostream>
using namespace std;
typedef int Integer;
const int MAX_N = 100;
Integer n, arr[MAX_N], visited[MAX_N];
void generatePermutations(Integer position) {
if (position == n + 1) {
for (Integer i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return;
}
for (Integer i = 1; i <= n; i++) {
if (visited[i] == 0) {
visited[i] = 1;
arr[position] = i;
generatePermutations(position + 1);
visited[i] = 0;
}
}
}
int main() {
cin >> n;
generatePermutations(1);
return 0;
}
链接描述