利用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恰恰相反。

附一个全排列字母输出的例子:

题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。

输入描述:
输入一个字符串,长度不超过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函数的更多相关文章

  1. STL中关于全排列next_permutation以及prev_permutation的用法

    这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...

  2. C++全排列函数next_permutation()和prev_permutation()

    头文件:#include<algorithm> * * * 1. next_permutation(): next_permutation()函数的返回类型是bool类型. 即:如果有一个 ...

  3. C++ STL, next_permutation用法。

    next_permutation 将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为 #include"iostream" #include"algorithm ...

  4. 打印全排列和stl::next_permutation

    打印全排列是个有点挑战的编程问题.STL提供了stl::next_permutation完美的攻克了这个问题. 可是,假设不看stl::next_permutation,尝试自己解决,怎么做? 非常自 ...

  5. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  6. 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数

    ---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...

  7. C++STL中的unique函数解析

    一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...

  8. STL之vector常用函数笔记

    STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...

  9. 「STL中的常用函数 容器」

    占个坑,下午在更 二分操作:lower_bound和upper_bound 存图/数列操作:vector容器 全排列:next_permutation和prev_permutation 字符串转数列: ...

随机推荐

  1. MySQL缺失mysql_config文件

    打算爬虫,安装mysqldb 结果使用pip安装出错 在centos-6.4上pip install mysql-python,报错如下[sentry@kjtest111 mysql-python]$ ...

  2. 基于centOS6.7搭建LAMP(httpd-2.4.18+mysql-5.5.47+php-5.6.16)环境

    首先确保系统可以联网.设置IP地址以及虚拟机安装linux在此略过.本文采用centos6.7 64位minimal版.php5.6.16.httpd-2.4.18.mysql-5.5.47版搭建la ...

  3. 20145212 实验五《Java网络编程》

    20145212 实验五<Java网络编程> 一.实验内容 1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成 ...

  4. perspective属性

    1. 目前只有safari和chrome浏览器支持 -webkit-perspective. 2. 单位为像素 { -webkit-perspective:500 } 该属性只影响子元素的的透视效果.

  5. Behavior Tree

    http://www.craft.ai/blog/bt-101-behavior-trees-grammar-basics/ https://github.com/libgdx/gdx-ai/wiki ...

  6. 无忧之道:Docker中容器的备份、恢复和迁移

    原创:LCTT https://linux.cn/article-5967-1.html译者: GOLinux本文地址:https://linux.cn/article-5967-1.html 201 ...

  7. 使用VMware 安装Linux CentOS7

    访问百度经验 http://jingyan.baidu.com/article/eae0782787b4c01fec548535.html 安装无忧..

  8. RPC-远程过程调用协议

    远程过程调用协议 同义词 RPC一般指远程过程调用协议 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要 ...

  9. 代码重构 & 常用设计模式

    代码重构 重构目的 相同的代码最好只出现一次 主次方法 主方法 只包含实现完整逻辑的子方法 思维清楚,便于阅读 次方法 实现具体逻辑功能 测试通过后,后续几乎不用维护 重构的步骤 1  新建一个方法 ...

  10. JS刷新页面总和!多种JS刷新页面代码!

    1)<meta http-equiv="refresh"content="10;url=跳转的页面">10表示间隔10秒刷新一次2)<scri ...