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=;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之全排列的更多相关文章
- unique && stl的全排列
stl的全排列: 看代码. #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- 组合数学 + STL --- 利用STL生成全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 【STL】全排列生成算法:next_permutation
C++/STL中定义的next_permutation和prev_permutation函数是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列. next_permutation函数 ...
- STL - next_permutation 全排列函数
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
- stl 生产全排列 next_permutation
#include<stdio.h>#include<algorithm>using namespace std;int main(){ int n,p[10]; scanf(& ...
- STL next_permutation 全排列
调用方法: ]={,,,}; )){ ;i<;i++) printf("%d ",arr[i]); puts(""); } 测试效果: 注:可以看到1 2 ...
- codevs 1229 数字游戏(可重集的全排列)
传送门 Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后 ...
- 力扣Leetcode 46. 全排列
全排列 给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...
- 多种方法实现实现全排列 + sort调用标准函数库函数的简述
全排列:所有不同顺序的元素组组成的一个集合.这里使用使用递归实现全排列. 使用递归算算法呢,首先我们先找一下结束的条件:我们要对一组元素(这里使用数字举例)实现全排列,临界条件就是递归到只有一个元素的 ...
随机推荐
- jquery的ajax实现方式
在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 首先我们看$.get(): .代码如下: $.get("test.jsp", { ...
- python 一些乱七八糟的东西
import random import os import sys import re class _is: def __init__(self,reg): self.cr=re.compile(r ...
- Kafka配置文档
http://kafka.apache.org/08/configuration.html
- linux下解压,压缩命令大全---方便新手查看
本文最后讲述如何在打包时排除某些文件 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --- ...
- Python小程序之动态修改Haproxy配置文件
需求如下: 1.动态的查询添加删除haproxy节点信息 2.程序功能:add(添加).Del(删除).Query(查询) 3.添加时实例字符串为: {'backend': 'www.oldboy. ...
- hashlib模块加密用法
hashlib 加密模块 hashlib.md5() 构建一个md5的对象,用于调用对象的update方法去加密 例子: import hashlib hash = hashlib.md5() h ...
- 【“10”力全开 游戏“Ti”厉害】ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB)
[“10”力全开 游戏“Ti”厉害]ZX53VE-新飞行堡垒笔记本(Windows 10 Home/新七代标压i7-7700HQ/GTX 1050Ti 4G/8G内存/1TB+128GB) http: ...
- 查找(二分、hash、桶)
先上一个最简单的题 1230 元素查找 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过. 输入描述 Input Description 第一行两个整数 n 和m. ...
- 作业执行器Job Executor
Job Executor 激活作业执行器 AsyncExecutor是一个组件,它管理线程池,来触发计时器和其他异步任务.其他实现也是可能的(例如使用消息队列,请参阅用户指南的高级部分). 默认情况下 ...
- poj 2242(已知三点求外接圆周长)
The Circumference of the Circle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8310 ...