next_permutation() 全排列函数

这个函数是STL自带的,用来求出该数组的下一个排列组合

相当之好用,懒人专用

适用于不想自己用dfs写全排列的同学(结尾附上dfs代码)

洛谷oj可去 P1008 三连击

注意:

  1. 使用前数组需要排序(升序)
  2. prev_permutation()是求前一个排列组合
  3. 数组 vector都可以,确定全排列的范围
#include <iostream>
#include <algorithm> //函数所需头文件
using namespace std;
int a[10];
void f(){
int t1,t2,t3;
t1=a[1]*100+a[2]*10+a[3]*1;
t2=a[4]*100+a[5]*10+a[6]*1;
t3=a[7]*100+a[8]*10+a[9]*1;
if(t1*2==t2&&t1*3==t3) cout<<t1<<" "<<t2<<" "<<t3<<endl;
}
int main(){
for(int i=1;i<=9;++i) a[i]=i; //sort
do{
f();
}while(next_permutation(a+1,a+10)); //prev_
return 0;
}

dfs全排列

#include <bits/stdc++.h>
using namespace std;
int a[10];
bool vis[10]={0}; //记录该数字是否被使用过
int n=9;
void dfs(int idx){
if(idx>n){ //边界输出
for(int i=1;i<=n;++i) cout<<a[i];
cout<<endl;
}
for(int i=1;i<=n;++i){
if(!vis[i]){
vis[i]=1;
a[idx]=i;
dfs(idx+1);
vis[i]=0; //回溯
}
}
}
int main(){
cin>>n;
dfs(1);
return 0;
}

next_permutation() 全排列函数的更多相关文章

  1. STL - next_permutation 全排列函数

    学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...

  2. C++ STL 全排列函数

    C++  全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...

  3. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  4. POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21268   Accepted: 8049 Description 题 ...

  5. C++中全排列函数next_permutation用法

    最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...

  6. C++ 全排列函数 std::next_permutation与std::prev_permutation

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  7. 全排列函数(next_permutation)

    顾名思义,这个函数就是用来求数组的全排列的,至于怎么用,看下面的介绍: 这是一个c++函数,包含在头文件algorithm里面,这个函数可以从当前的数组的大小按照字典序逐个递增的顺序排列 看下面的模板 ...

  8. 全排列函数 nyoj 366(next_permutation()函数)

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  9. 全排列函数(next_permutation())

    平常需要全排列的时候,一般都是dfs然后字符串匹配啥的……今天看题解的时候突然发现了这个神器. next_permutation()函数在c++的algorithm库里,作用是传入一个数组,输出这个数 ...

随机推荐

  1. Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (64)

    错误提示: Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupporte ...

  2. Nowcoder Hash Function ( 拓扑排序 && 线段树优化建图 )

    题目链接 题意 : 给出一个哈希表.其避免冲突的方法是线性探测再散列.现在问你给出的哈希表是否合法.如果合法则输出所有元素插入的顺序.如果有多解则输出字典序最小的那一个.如果不合法则输出 -1 分析 ...

  3. CMS 与 框架

    Framework:框架.是整合的工具集,基于编程语言.可以帮助我们快速开发网站.比较常见的是J2EE(基于Java),Symfony2(基于PHP),Django(基于Python),Ruby on ...

  4. TCP慢启动

    慢启动定义 慢启动,是传输控制协议使用的一种阻塞控制机制.慢启动也叫做指数增长期.慢启动是指每次TCP接收窗口收到确认时都会增长.增加的大小就是已确认段的数目.这种情况一直保持到要么没有收到一些段,要 ...

  5. Oracle用函数或PIVOT实现行转列

    原数据: 目标数据: 实现代码: SELECT YEAR AS "年", SUM (DECODE (QUATER, 1, RATE, 0)) AS "季度一", ...

  6. 半径R覆盖最多点

    struct point { double x, y; }; point p[N]; struct alpha { double v; bool flag; bool friend operator ...

  7. ionic1使用imagepicker在安卓手机上闪退问题

    在上一篇文章中,提到了如何在ionic1中使用imagepicker插件,并且实现该插件显示中文(汉化)问题有兴趣可以看看:ionic1使用ImagePicker插件并且显示中文(汉化) 1.这次要解 ...

  8. Idea如何生成JPA的相关model,以及运行JPA项目的时候启动错误

    1.如何生成JPAmodel 按照顺序执行下面的步骤 为指定的项目添加JPA的配置,这样之后生成的model就会在指定的项目内 选择JPA之后默认不用操作直接添加 没有Persistence的可以在w ...

  9. 生成json文件写入本地

    public class Json { public static void main(String[] args) { String fullPath = null; //例如:fullPath=& ...

  10. Java基础教程免费分享

    这是我自己早前听课时整理的Java全套知识,适用于初学者,也可以适用于中级进阶的人,你们可以下载,我认为是比较系统全面的,可以抵得上市场上90%的学习资料.讨厌那些随便乱写的资料还有拿出来卖钱的人!在 ...