描述

使用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=;i<vec.size();i++)
{
cout<<vec[i];
if( i!=vec.size()-)
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 ;
}

STL之全排列的更多相关文章

  1. unique && stl的全排列

    stl的全排列: 看代码. #include<iostream> #include<cstdio> #include<algorithm> #include< ...

  2. 组合数学 + STL --- 利用STL生成全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  3. 【STL】全排列生成算法:next_permutation

    C++/STL中定义的next_permutation和prev_permutation函数是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列. next_permutation函数 ...

  4. STL - next_permutation 全排列函数

    学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...

  5. stl 生产全排列 next_permutation

    #include<stdio.h>#include<algorithm>using namespace std;int main(){ int n,p[10]; scanf(& ...

  6. STL next_permutation 全排列

    调用方法: ]={,,,}; )){ ;i<;i++) printf("%d ",arr[i]); puts(""); } 测试效果: 注:可以看到1 2 ...

  7. codevs 1229 数字游戏(可重集的全排列)

    传送门 Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间.  这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后 ...

  8. 力扣Leetcode 46. 全排列

    全排列 给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...

  9. 多种方法实现实现全排列 + sort调用标准函数库函数的简述

    全排列:所有不同顺序的元素组组成的一个集合.这里使用使用递归实现全排列. 使用递归算算法呢,首先我们先找一下结束的条件:我们要对一组元素(这里使用数字举例)实现全排列,临界条件就是递归到只有一个元素的 ...

随机推荐

  1. shell编程---变量赋值

    echo $filen | awk -F. '{print $3}'  怎么把上边这行脚本产生的字串赋给一个变量啊,实际上会产生一个数, 这个变量用来存这个数.格式应该是怎么写的? a=`echo $ ...

  2. MySQL rpm 版本安装

     准备: [root@localhost moudles]# ls MySQL-client-5.6.36-1.linux_glibc2.5.x86_64.rpm MySQL-server-5.6.3 ...

  3. JQuery如何监听DIV内容变化

    这几天在做一个微博的接入,需要判断微博是否被关注,要检查微博标签的DIV是否有“已关注”的字符,但这个DIV的内容是微博JSSDK动态生 成.$("#id").html()是获取不 ...

  4. HDU1016 素数环---(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 Sample Input 6 8   Sample Output Case 1: 1 4 3 2 5 6 ...

  5. windows主机控制

    一.开关机控制 using System.Runtime.InteropServices; //注销.关机.重启 class shutdown { [StructLayout(LayoutKind.S ...

  6. bzoj 1011 近似估计

    开始看这道题的时候没什么思路,后来忍不住看了题解,发现自己真是水啊... 自从学OI来第一次看到用约等的题 首先我们设w[i]为第i个星球的答案,g[i]为第i个星球受到1-g[i]个星球的引力 那么 ...

  7. 上传代码到github上

    初始化 git init 添加远程仓库 git remote add origin[仓库名] 仓库地址 添加文件 git add . 本地提交 git commit -m 'message' 拉去远程 ...

  8. css的class, id等常用命名规则

    CSS的class.id.css文件名的常用命名规则        (一)常用的CSS命名规则 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sid ...

  9. [转]CreateDIBitmap与CreateDIBSection

    首先明确最主要区别:CreateDIBitmap创建的是设备相关位图句柄 - HBITMAP.                               CreateDIBSection创建的是设备 ...

  10. 在servlet中返回json数据

    在servlet: String name = new tring(request.getParameter("name").getBytes("iso8859-1&qu ...