zoukankan      html  css  js  c++  java
  • STL之全排列

    描述

    使用STL中的next_permutation函数输出一个序列的全排列。

    部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

     

    int main()
    {
        vector<int> vec;
        int n, x;
        cin>>n;
        while(n--)
        {
            cin>>x;
            vec.push_back(x);
        }
        Permutation(vec);
        return 0;
    }

    输入

    第一行为一个正整数n(n<8)。

    第二行有n个整数。

    输出

    从小到大顺序(第一个数小的先输出,如果相等则第二个小的先输出,以此类推)输出全排列。

    样例输入

     3
    3 1 2

    样例输出

    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    void Permutation(vector<int> &vec)
    {
        sort(vec.begin(),vec.end());
        do
        {
            int i;
            for( i=0;i<vec.size();i++)
            {
                cout<<vec[i];
                if( i!=vec.size()-1)
                    cout<<" ";
            }
            cout<<endl;
        }while(next_permutation(vec.begin(),vec.end()));
    }
    int main()
    {
        vector<int> vec;
        int n, x;
        cin>>n;
        while(n--)
        {
            cin>>x;
            vec.push_back(x);
        }
        Permutation(vec);
        return 0;
    }

     

  • 相关阅读:
    vue 实现左侧分类列表,右侧文档列表
    C# string数组与list< string >的相互转换
    c# List<string>的用法
    类数组 数组
    事件
    js封装方法和浏览器内核
    dom
    try...catch es5
    data对象 定时器
    call apply 原型 原型链
  • 原文地址:https://www.cnblogs.com/andrew3/p/8859347.html
Copyright © 2011-2022 走看看