next_permutation

来源:互联网 发布:域名劫持查询 编辑:程序博客网 时间:2024/05/20 01:13

参考代码

#include<cstdio>#include<algorithm>using namespace std;int main(){int n,p[10];scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&p[i]);sort(p,p+n);//排序,得到p的最小排列 do{for(int i=0;i<n;i++)printf("%d ",p[i]);//输出排列p printf("\n");}while(next_permutation(p,p+n));//求下一个排列 return 0;}

示例输入1

43 4 2 1

示例输出1

1 2 3 41 2 4 31 3 2 41 3 4 21 4 2 31 4 3 22 1 3 42 1 4 32 3 1 42 3 4 12 4 1 32 4 3 13 1 2 43 1 4 23 2 1 43 2 4 13 4 1 23 4 2 14 1 2 34 1 3 24 2 1 34 2 3 14 3 1 24 3 2 1

示例输入2

43 1 1 2

示例输出2

1 1 2 31 1 3 21 2 1 31 2 3 11 3 1 21 3 2 12 1 1 32 1 3 12 3 1 13 1 1 23 1 2 13 2 1 1
原创粉丝点击