zoukankan      html  css  js  c++  java
  • 全排列 next_permutation()

    全排列

    next_permutation()

    在头文件<algorithm>里面有如下代码:

    int a[];
    do
    {
    
    }
    while(next_permutation(a,a+n));

    例子:

    #include<bits/stdc++.h>
    #include<set>
    #include<vector>
    #include<deque>
    #include<map>
    #include<queue>
    using namespace std;
    int main(){
        int n;
        cin>>n;
        int * p = new int[n];
        for (int i = 0; i < n; i++)
        {
            cin>>p[i];
        }
        sort(p,p+n);
        do
        {
            for (int i = 0; i < n; i++)
            {
                cout<<p[i]<<" ";
            }
            cout<<endl;        
        } while (next_permutation(p,p+n));
    
        cout<<endl;
        string s = "aba";
        sort(s.begin(), s.end());
        do {
            cout << s << '
    ';
        } while(next_permutation(s.begin(), s.end()));
    
    
    }

    输出:

    3
    1 2 3
    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1
    
    aab
    aba
    baa

    prev_permutation

    例子

    #include<bits/stdc++.h>
    #include<set>
    #include<vector>
    #include<deque>
    #include<map>
    #include<queue>
    using namespace std;
    int main(){
        int n;
        cin>>n;
        int * p = new int[n];
        for (int i = 0; i < n; i++)
        {
            cin>>p[i];
        }
        sort(p,p+n,greater<int>());
        do
        {
            for (int i = 0; i < n; i++)
            {
                cout<<p[i]<<" ";
            }
            cout<<endl;        
        } while (prev_permutation(p,p+n));
    
        cout<<endl;
        string s = "aba";
        sort(s.begin(), s.end());
        do {
            cout << s << '
    ';
        } while(prev_permutation(s.begin(), s.end()));
    
    }

    输出:

    3
    1 2 3
    3 2 1
    3 1 2
    2 3 1
    2 1 3
    1 3 2
    1 2 3
    
    aab

     

    因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/13700864.html

  • 相关阅读:
    shell得到两个文件的差集
    shell 单引号&双引号的使用
    kubernetes session and 容器root权限
    docker 使用网络以及容器互联
    倒计时练习
    会话控制
    XML
    AJAX实现搜索智能提示
    弹窗显示详情练习
    三级联动
  • 原文地址:https://www.cnblogs.com/BlairGrowing/p/13700864.html
Copyright © 2011-2022 走看看