利用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. coreseek 中文搜索和高亮

    配置文件 # # Minimal Sphinx configuration sample (clean, simple, functional) # source post { type = mysq ...

  2. 关键词提取1-C#

    C# 中文分词算法(实现从文章中提取关键字算法) using System;using System.IO;using System.Text;using System.Collections;usi ...

  3. AspectJ基础学习之三HelloWorld(转载)

    AspectJ基础学习之三HelloWorld(转载) 一.创建项目 我们将project命名为:aspectjDemo.然后我们新建2个package:com.aspectj.demo.aspect ...

  4. Django笔记-MySQL初次使用设置

    以下为个人学习时的笔记,正在完善中........... [1]启动服务 [root@bogon /]# service mysqld start正在启动 mysqld: [确定] [root@bog ...

  5. java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别

    java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别   以前一直没有注意过这个问题,前两天机缘巧合上网查了一下,然后自 ...

  6. oss cmd

    osscmd是基于python 2.5.4(其他版本没有试过),用来操作OSS的,可使用命令行来上传和下载文件. 下载地址:http://storage.aliyun.com/leo/osscmd.t ...

  7. YII2数据库依赖缓存

    首先配置一下缓存,自己选择是用文件缓存还是数据库缓存等. 'cache' => [ 'class' => 'yii\caching\FileCache', ], 然后就可以通过 Yii:: ...

  8. spring之BeanFactoryAware接口

    springBeanFactoryAware (转)要直接在自己的代码中读取spring的bean,我们除了根据常用的set外,也可以通过spring的BeanFactoryAware接口实现,只要实 ...

  9. js中的全局变量和静态变量的使用, js 的调试?- 如果js出错, js引擎 就会停止, 这会 导致 后面的 html中 refer 该函数时, 会报错 函数为定义!!

    效果里面的函数, 如show, hide,slideDown等, 这些都叫 "效果"函数, 但是里面可以包含动画, 也可以 不包含动画. 动画,是指 元素 的内容 是 逐渐 显示/ ...

  10. CString

    CString gray("Gray"); CString cat("Cat"); CString graycat = gray + cat;   与其用 sp ...