# 全排列——STL教你做人

> 看见很多人用DFS

> 但我今天想说的是，STL教你做人

> 昨天我和dalao比谁的速度快

> 结果一个是10.07，一个是10.48

> STL完胜

#include <algorithm>

bool next_permutation(iterator start,iterator end)

cpp
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[3]={1,2,3};
do
{
cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
}while(next_permutation(num,num+3));
return 0;
}


1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

1 2 3
2 1 3

2 3 1
3 1 2
3 2 1

cpp
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
//freopen("fuck.in", "r", stdin);
//freopen("fuck.out", "w", stdout);
int n;
scanf("%d",&n);
int a[n + 5];
for(int i = 0; i <= n - 1;i++)
a[i] = i + 1;
do
{
for(int i = 0;i <= n - 1;i++)
printf("%5d",a[i]);
printf("\n");
}while(next_permutation(a,a + n)); //函数的运用
return 0；
//fclose(stdin);
//fclose(stdout);
}


cpp
#include<bits/stdc++.h>
using namespace std;int main(){int n;scanf("%d",&n);int a[n + 5];for(int i = 0; i <= n - 1;i++) a[i] = i + 1;do{for(int i = 0;i <= n - 1;i++)printf("%5d",a[i]);printf("\n");}while(next_permutation(a,a + n)); return 0;}


• 评论

• 下一篇