next_permutation() 全排列函数
next_permutation() 全排列函数
这个函数是STL自带的,用来求出该数组的下一个排列组合
相当之好用,懒人专用
适用于不想自己用dfs写全排列的同学(结尾附上dfs代码)
洛谷oj可去 P1008 三连击
注意:
- 使用前数组需要排序(升序)
 - prev_permutation()是求前一个排列组合
 - 数组 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() 全排列函数的更多相关文章
- STL - next_permutation 全排列函数
		
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
 - C++  STL  全排列函数
		
C++ 全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...
 - 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards   (STL全排列函数)
		
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
 - POJ1833 排列 调用全排列函数 用copy函数节省时间  即使用了ios同步代码scanf还是比较快
		
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21268 Accepted: 8049 Description 题 ...
 - C++中全排列函数next_permutation用法
		
最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...
 - C++ 全排列函数 std::next_permutation与std::prev_permutation
		
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
 - 全排列函数(next_permutation)
		
顾名思义,这个函数就是用来求数组的全排列的,至于怎么用,看下面的介绍: 这是一个c++函数,包含在头文件algorithm里面,这个函数可以从当前的数组的大小按照字典序逐个递增的顺序排列 看下面的模板 ...
 - 全排列函数 nyoj 366(next_permutation()函数)
		
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
 - 全排列函数(next_permutation())
		
平常需要全排列的时候,一般都是dfs然后字符串匹配啥的……今天看题解的时候突然发现了这个神器. next_permutation()函数在c++的algorithm库里,作用是传入一个数组,输出这个数 ...
 
随机推荐
- EasyUI DataGrid undefined处理
			
处理undefined var val = null; console.log(val); console.log(val || ""); val = undefined; con ...
 - C# 在知道对象时编译json  而不调用json类
			
StringBuilder sb = new StringBuilder(); sb.Append('['); foreach (va ...
 - 【python】windows更改jupyter notebook(ipython)的默认打开工作路径
			
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
 - 【Spark机器学习速成宝典】模型篇04朴素贝叶斯【Naive Bayes】(Python版)
			
目录 朴素贝叶斯原理 朴素贝叶斯代码(Spark Python) 朴素贝叶斯原理 详见博文:http://www.cnblogs.com/itmorn/p/7905975.html 返回目录 朴素贝叶 ...
 - 网络安全监控实战(一):Snort,Wazuh&VT
			
https://cloud.tencent.com/developer/news/222711
 - java基本算法
			
1.链表 链表用来存储数据,由一系列的结点组成.这些结点的物理地址不一定是连续的,即可能连续,也可能不连续,但链表里的结点是有序的.一个结点由数据的值和下一个数据的地址组成.一个链表内的数据类型可以是 ...
 - Oracle Rac 测试
			
#还是使用之前的脚步来进行测试 #Author : Kconnie Pong Oracle@PONGDB:~> more load_balance.sh #!/bin/bash ..} do ...
 - leetcode-easy-listnode-19 remove nth node from end of list
			
mycode 88.29% 关键是一定要head前新建一个节点,否则要分类讨论很多次来避免slow或者fast出现None.next的错误 # Definition for singly-linke ...
 - loggin模块,错误日志模块
			
# 记录用户行为或者报错信息 import logging # 配置错误日志有两种方法 # 方法1:通过basicconfig # 配置简单.但是能做的事情少,解决不了中文乱码,不能同时输出到屏幕和文 ...
 - tomcat服务器经常需要重启
			
程序看着运行正常,但是点击几下就没反应了. 可能原因:1.tomcat内存不足 2.程序中有资源未释放.比如session(hibernate的)等(需要close)