The full permutation next_permutation of function is recommended by of
- 2020-05-17 05:58:00
- OfStack
This is an c++ function that is included in the header file < algorithm > Inside, below is the basic format.
1 int a[];
2 do{
3
4 }while(next_permutation(a,a+n));
The following code produces a full array of 1 to n
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n;
while(scanf("%d",&n)&&n){
int a[1000];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);// You can test it yourself 1 Delete the following results
do{
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}while(next_permutation(a,a+n));
}
return 0;
}
For example, the input
3
1 0 2
If you have sort ()
The output is
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0
If there is no
The output of
1 0 2
1 2 0
2 0 1
2 1 0
Many combinations can be found missing.
However, a careful comparison of the various combination methods and the output with or without sort() shows that the function nex
rmutation() is generated in lexicographical order, increasing from the current lexicographical order in the array to the maximum lexicographical order.