这是一个计算排列组合的函数
next_permutaion 下一个排列
生成一个字典序更大的排列

Rearranges the elements in the range [first,last) into the next lexicographically greater permutation.

返回值是bool型
如果仍然可以找到字典序更大的排列,则返回true,并且数组(假定是数组)中变成一个新的排列,比如{2,1,3};如果不能再找到一个字典序更大的排列就返回false,而且数组还是最小的那个排列,比如{1,2,3}。

If the function can determine the next higher permutation, it rearranges the elements as such and returns true. If that was not possible (because it is already at the largest possible permutation), it rearranges the elements according to the first permutation (sorted in ascending order) and returns false.

Return value
true if the function could rearrange the object as a lexicographicaly greater permutation.
Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).

resource:https://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_permutation

Cplusplus官网给出的例程

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sortint main () {int myints[] = {1,2,3};std::sort (myints,myints+3);std::cout << "The 3! possible permutations with 3 elements:\n";do {std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';} while ( std::next_permutation(myints,myints+3) );std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';return 0;
}

输出结果

The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3

分析
这里使用next_permutation之前使用sort函数对数组进行排序,以便返回所有的排列。

Leetcode46. 全排列
题目链接:https://leetcode-cn.com/problems/permutations/

给定一个 没有重复 数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

分析
由于没有重复数字,这样的排列可以直接使用STL中的next_permutation函数,输出是按照升序输出。
同样需要注意的是:这里使用next_permutation之前使用sort函数对数组进行排序,以便返回所有的排列。
如果next_permutation 为真,则把此时的nums数组中的元素添加到tmp数组中,直到找到所有的排列。
AC代码

class Solution {public:vector<vector<int>> permute(vector<int>& nums) {if(nums.size()==0) return {};sort(nums.begin(),nums.end());//从小到大排序vector<vector<int>> tmp;//结果数组do{tmp.push_back(nums);  //添加到结果数组中  }while(next_permutation(nums.begin(),nums.end()));//如果还有字典序更大的排列的话return tmp;}
};

一道题目
1、题目描述

Ray又对数字的列产生了兴趣:

现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。

输入

输入一组数据,包含四个整数,代表四张卡片上的数字(0<=数字<=9)

输出

按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。(注意:行末不得有多余的空格和换行)并且不能有前导零

样例输入

1 1 2 3

样例输出

1123 1132 1213 1231 1312 1321

2113 2131 2311

3112 3121 3211

分析
使用一个tmp来保存最高位,方便换行。
另外需要一个cnt来判断是否是每一行的第一个排列。

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
int a[10];int main(){for(int i=0;i<4;i++)cin>>a[i];//全零直接输出即可。sort(a,a+4);//没有前导零的处理 if(a[0]==0){if(a[1]!=0) swap(a[1],a[0]);else {if(a[2]!=0) swap(a[0],a[2]);else if(a[3]!=0) swap(a[0],a[3]);}}int cnt=1;//第几个数字 int tmp=a[0];//最高位是否相等 int num=0;//统计没有前导零的排列的个数 do{num++;if(tmp!=a[0]){//换行 cout<<endl;//    cout<<"上一行的排列个数:"<<cnt-1<<endl; cnt=1;tmp=a[0];//更新tmp ,a[0]跑的快 }if(cnt==1){//每行第一个排列 cout<<a[0]<<a[1]<<a[2]<<a[3];//这里不加空格的原因是,要求每行不能有多余空格 tmp=a[0];cnt++;//后移   }else{//输出每行后续排列 cout<<" "<<a[0]<<a[1]<<a[2]<<a[3];cnt++;}}while(next_permutation(a,a+4));//cout<<endl;//cout<<"上一行的排列个数:"<<cnt-1<<endl; //cout<<"没有前导零的所有的排列个数为:"<<num<<endl;}

C++STL之next_permutation使用相关推荐

  1. 使用STL的next_permutation函数生成全排列(C++)

    下午研究了一下全排列算法,然后发现C++的STL有一个函数可以方便地生成全排列,这就是next_permutation 在C++ Reference中查看了一下next_permutation的函数声 ...

  2. C++STL之next_permutation()函数使用

    如n==3时的全排列为 123 132 213 231 312 321 代码如下: #include #include using namespace std; int main(){ int a[1 ...

  3. 第1期:牛客竞赛语法入门班数组栈、队列和stl习题

    提纲 A 老子的全排列呢 使用了next_permutation [用法总结]C++ STL中 next_permutation函数的用法 #include<iostream> #incl ...

  4. Leetcode46全排列DFS

    链接 题目大意:给定一个数组,求出所有的全排列. 分析 DFS和回溯的方法. 回溯算法的核心 选择列表:表示当前可做的选择 路径:记录做过的选择 结束条件:遍历到树的底层,在这里是选择列表为空的时候. ...

  5. BestCoder-Round#38

    1001 描述 给出四个三维坐标下的点, 判定是否为正方形. 分析 用向量的数量积来判定是否垂直, 再判断长度. 我是在纸上画出了A(3,2)=6A(3,2)=6 种情况然后暴力枚举判断是否为正方形. ...

  6. 微软官网html官方文档,微软官方的.net系列文档

    获取Windows操作系统的CPU使用率以及内存使用率 此功能参考了ProcessHacker项目的代码. 声明定义 typedef struct _UINT64_DELTA { ULONG64 Va ...

  7. 每天一道LeetCode-----获取无重复项/有重复项序列的全排列

    原题链接Permutations 要求是输出给定序列的全排列,序列中不包含重复元素 STL中有next_permutation函数可以获取当前序列的下一个排列,使用起来也很简单,先对序列按递增顺序排序 ...

  8. 【转】全排列算法非递归实现和递归实现

    来源:http://blog.csdn.net/e3399/article/details/7543861 (一)递归的全排列算法 (A.B.C.D)的全排列为 1.A后面跟(B.C.D)的全排列 2 ...

  9. 牛客题霸 [ 有重复项数字的所有排列] C++题解/答案

    牛客题霸 [ 有重复项数字的所有排列] C++题解/答案 题目描述 给出一组可能包含重复项的数字,返回该组数字的所有排列. 题解: 很多人应该都是用的递归方式来做,这里介绍一个stl的next_per ...

最新文章

  1. 如何按值对字典排序?
  2. Conficker-AE的处理方法
  3. Eureka 服务注册与发现01——单机版
  4. 北斗导航 | 读取ground truth data(python源代码)
  5. 数据库优化:8 种常见的SQL错误用法
  6. cad2016珊瑚_预测有马的硬珊瑚覆盖率
  7. 群体智能之粒子群优化(PSO)
  8. 大学c语言程序设计上机题库,c语言上机题库(c语言程序设计基础题库)
  9. 苹果CMS v10详细安装教程+官方原版源码分享
  10. DB2完整的SQLCODE和SQLState错误信息
  11. 如何使用ccs软件创建工程
  12. 解除隐藏文件cmd命令_用DOS命令怎么取消隐藏文件夹
  13. GIS应用技巧之景观格局分析(四)
  14. 如何用Python给自己做一个年终总结
  15. 项目动态|Apache Pulsar 2.10.0 版本介绍
  16. 毕业论文关键字HTML5,毕业论文关键词的选择
  17. Hadoop生态系统——HiveQL操作实战
  18. gnu make 手册 学习笔记 C语言 / C++ 构建工具 part.5 函数 控制语句
  19. 在线学习(Online learning)与离线学习(Offline learning)
  20. 【校招VIP】互联网校招项目实习对项目的要求不重要?大错特错!你忽略掉的项目考察重点都在这里!

热门文章

  1. Xamarin IOS – hello word
  2. Python基本语法(基于3.x)
  3. Java Android 32位16位 MD5加密
  4. 第二十三模板 18标准模板库
  5. 关于C#中用access做数据库,使用like语句的问题
  6. HTML如何在按钮右侧显示div,如何实现点击页面中的一个按钮相应位置的div显示隐藏?...
  7. python3转为unicode编码_「小技能get记」· 如何用python3解码unicode
  8. bat 域 本机管理员密码_Windows域中特殊的用户-计算机对象攻防
  9. iis php5.3 mysql_Win2008 R2配置IIS7.5+PHP Manager+PHP5.3+Mysql5.5+Wincache
  10. 电脑qq文件服务器地址,在电脑上接收QQ地理位置打不开,提示获取不到详细地址...