STL的全排列函数
next_permutation(a + 1, a + 1 + n);
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define inf 0x3f3f3f3f int a[100]; int main() { int n; cin >> n; for (int i = 1; i <= n;i++){ a[i] = i; } do{ for (int i = 1;i<=n;i++){ cout << a[i] << " "; } cout << endl; } while (next_permutation(a + 1, a + 1 + n)); }
输入:
4
这是输出:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1