STL中的全排列next_permutation
next_permutation的函数声明:#include
bool next_permutation( iterator start, iterator end);
next_permutation函数的返回值是布尔类型,在STL中还有perv_permutation()函数
[cpp] view plain copy
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- string str;
- cin >> str;
- sort(str.begin(),str.end());
- while (next_permutation(str.begin(), str.end()))
- cout << str << endl;
- return 0;
- }
next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。
而prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。
//@auther Yang Zongjun
#include
#include
#include
#include
#include
#include using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1100000;
const int INF = 2100000000;
int n, per[MAXN];void permutation(int n)
{for(int i = 0; i < n; i++){per[i] = i;}do{for(int i = 0; i< n; i++)cout << per[i] << " ";cout << endl;}while(next_permutation(per, per + n));
}int main()
{//freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);cin >> n;permutation(n);return 0;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
