STL的next_permutation函数可以求出某个特定序列的下一个排列,当然,如果对一个给定序列,排序之后可以轻松求出全排列......
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<map> 7 #include<set> 8 #include<vector> 9 #include<sstream> 10 using namespace std; 11 #define ll long long 12 const int inf=99999999; 13 const int mod=1e9+7; 14 //const int maxn=; 15 int num[100]; 16 int main() 17 { 18 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 19 20 int n; 21 cin>>n; 22 23 for(int i=0;i<n;i++) 24 cin>>num[i]; 25 26 sort(num,num+n); 27 28 cout<<"全排列 :"<<endl; 29 do 30 { 31 for(int i=0;i<n-1;i++) 32 cout<<num[i]<<" "; 33 cout<<num[n-1]<<endl; 34 } 35 while(next_permutation(num,num+n)); 36 37 return 0; 38 }
原文地址:https://www.cnblogs.com/xwl3109377858/p/10986717.html
时间: 2024-11-05 14:58:44