今天看书了发现了一个STL中的排列函数,next_permutation
给出两个示例
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,p[10];
cin>>n;
for(int i=0;i<n;++i)
cin>>p[i];
sort(p,p+n);
do //注意这步,如果是while循环,则需要提前输出
{
for(int i=0;i<n;++i)
cout<<p[i];
cout<<endl;
}while(next_permutation(p,p+n));
system("pause");
return 0;
}
Second
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
sort(str.begin(), str.end());
cout << str << endl;
while (next_permutation(str.begin(), str.end()))
{
cout << str << endl;
}
system("pause");
}