oi学习之stl next_permutation()

位置

#include<algorithm>

签名

#include <algorithm>
bool next_permutation( iterator start, iterator end );
//The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

方法

  1. c++
    ```cpp
    #include <iostream>
    #include <algorithm>
    #include <string/></algorithm></iostream></li>
    </ol>
    <p>using namespace std;</p>
    <p>int main()
    {
    string str;
    cin » str;
    sort(str.begin(), str.end());
    cout « str « endl;
    while (next_permutation(str.begin(), str.end()))
    {
    cout « str « endl;
    }
    return 0;
    }</p>
    <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>2. c
    ```c
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define MAX 100

    using namespace std;

    int main()
    {
    int length;
    char str[MAX];
    gets(str);
    length = strlen(str);
    sort(str, str + length);
    puts(str);
    while (next_permutation(str, str + length))
    {
    puts(str);
    }
    return 0;
    }

微海报分享