STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置,
直接求到第一个不按升序排列的序列。
#include <iostream>
#include <algorithm> /// next_permutation, sort
#define MAX 100
using namespace std; int main() {
int myints[MAX],n;
cin >> n;
for (int i = ; i < n; i++)
{
cin >> myints[i];
}
sort(myints, myints + n); do {
for (int i = ; i < n; i++)
{
cout << myints[i] <<" ";
}
cout << endl;
} while (next_permutation(myints, myints + n)); ///获取下一个较大字典序排列 system("pause");
return ;
}

同理,prev_permutation恰恰相反。
附一个全排列字母输出的例子:
题目描述
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
#include "iostream"
#include "string"
#include "vector"
#include "algorithm" using namespace std; void Permutation(string str) {
vector<char> sstr;
for (int i = ; i < str.length(); i++)
sstr.push_back(str[i]); do {
for (int i = ; i < str.length(); i++)
{
cout << sstr[i] << " ";
}
cout << endl; } while (next_permutation(sstr.begin(), sstr.end())); } int main() {
string str;
while (cin >> str)
{
Permutation(str);
}
return ;
}
STL next_permutation和prev_permutation函数的更多相关文章
- STL中关于全排列next_permutation以及prev_permutation的用法
这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...
- C++全排列函数next_permutation()和prev_permutation()
头文件:#include<algorithm> * * * 1. next_permutation(): next_permutation()函数的返回类型是bool类型. 即:如果有一个 ...
- C++ STL, next_permutation用法。
next_permutation 将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为 #include"iostream" #include"algorithm ...
- 打印全排列和stl::next_permutation
打印全排列是个有点挑战的编程问题.STL提供了stl::next_permutation完美的攻克了这个问题. 可是,假设不看stl::next_permutation,尝试自己解决,怎么做? 非常自 ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- C++STL中的unique函数解析
一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...
- STL之vector常用函数笔记
STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...
- 「STL中的常用函数 容器」
占个坑,下午在更 二分操作:lower_bound和upper_bound 存图/数列操作:vector容器 全排列:next_permutation和prev_permutation 字符串转数列: ...
随机推荐
- e_msg_c_gs_enter_gs_req
e_msg_c_gs_enter_gs_req 就是到GS上验证客户端发送的token是否存在,如果存在返回成功,并把该token删除
- git 删除远程分支
http://www.cnblogs.com/shiningrise/archive/2013/03/12/2956779.html 一不小心把本地的临时分支push到server上去了,想要删除.一 ...
- guid正则表达
a-fA-F0-9 加上下划线 _ 可以用 \w 来代替. ^\w{8}-(\w{4}-){3}\w{12}$ 如果不可以用下划线, 0-9 用 \d 代替 a-fA-F 就用其中一个 a-f,然后匹 ...
- javascript生成二维码
参考: http://www.w3dev.cn/article/20140617/javascript-create-QR-code.aspx
- 普通浏览器GET请求与Ajax的GET请求的区别
看图 普通浏览器请求 Ajax请求
- nginx -s reload失败
在重启nginx时报错误. wangkongming@ThinkPad-T410 ~ $ sudo /usr/sbin/nginx -s reload nginx: [error] invalid P ...
- connect() failed (111: Connection refused) while connecting to upstream
配置好lamp后,在浏览器中运行程序后,出现上面的错误. 转自:http://www.xuejiehome.com/blread-1828.html I'm experiencing 502 gate ...
- 配置lamp中的apache
root@komiles-VirtualBox:/etc/apache2/sites-available# service apache2 restart * Restarting web serve ...
- php分页代码简单实现
版权声明:本文为博主原创文章,未经博主允许不得转载. 数据库操作类代码:mysqli.func.php <?php // 数据库连接常量 define('DB_HOST', 'localhost ...
- 使用DOS比较两个txt文件的差异
将两个文件放入到同一个文件夹下 DOS下提供了FC命令 点击开始->运行->输入cmd,进入DOS下,进入指定目录,输入FC a.txt b.txt进行比较,下面会显示出之间的差异