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 字符串转数列: ...
随机推荐
- Docker create image
Dockerfile FROM java:8 MAINTAINER dudu ADD springts_1-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 ENTRYPO ...
- [转]h5页面测试总结
转自http://www.blogjava.net/qileilove/archive/2014/07/24/416154.html?utm_source=tuicool H5页面测试总结 其实经过几 ...
- iocp还是select
上一个项目libevent应该是select,现在libuv是iocp,都知道Windows下iocp比select效率高,boost asio 也是iocp,但具体使用select和iocp发现没有 ...
- Integer 与int 赋值比较
测试代码: @Test public void IntegerTest() { Integer i01 = 59; int i02 = 59; Integer i03 = Integer.valueO ...
- javaWeb加载Properties文件
public static Properties loadProps(String fileName) { Properties props = null; InputStream is = null ...
- C++中dynamic_cas操作符的工作原理
http://stackoverflow.com/questions/13783312/how-does-dynamic-cast-work http://publib.boulder.ibm.com ...
- jquery特效收藏
js网址收藏: 懒人图库:www.lanrentuku.com 懒人之家:http://www.lanrenzhijia.com/jquery/list_5_2.html 1.UI下载:http:// ...
- js控制全屏窗口
<script src="__PUBLIC__/Js/jquery.min.js"></script> <script type="text ...
- HTML5基本特性和新功能
HTML5的基本特征 1.向前兼容性 核心理念——平滑过渡! 不支持html5的浏览器可以向前兼容,并不会影响web内容的显示! 2.跨平台运行性 从pc浏览器到手机.平板电脑,甚至是智能电视. 只要 ...
- 修改wampserver 默认localhost 和phpmyadmin 打开链接
在wamp上 左键打开localhost 自定义端口的话 或者其他网址 需要以下修改(同样访问phpmyadmin修改也是这个地方) 修改文件路径 D:\wamp\wampmanager.tpl 搜索 ...