#include<bits/stdc++.h>
using namespace std;
int num = 0;
void swap(int A[], int x, int y) {
int temp = A[x];
A[x] = A[y];
A[y] = temp;
}
void printA(int A[], int length) {
for (int i = 0; i < length; i++) {
cout << A[i] << " ";
}
cout << endl;
}
void next_permutation(int A[], int begin, int end) {
if (begin == end) {
num++;
printA(A, end + 1);
}
else {
for (int i = begin; i <= end; i++) {
swap(A, begin, i);
next_permutation(A, begin + 1, end);
swap(A, begin, i);
}
}
}
int main() {
int A[] = { 1,2,3,4 };
next_permutation(A, 0, 2);
cout << "总数是:" << num << endl;
return 0;
}
测试结果: