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库里,作用是传入一个数组,输出这个数 ...
随机推荐
- 导数与微分简单总结(updated)
只讲一些导数在OI中的简单应用,特别基础的东西,不会很详细也不会很全面. 导数的定义 设函数\(y=f(x)\)在点\(x_0\)的某个邻域内有定义,当自变量\(x\)在\(x_0\)处有增量\(Δx ...
- iview之avatar组件
avatar组件是一个获取网络路径图片的组件 它再vue中使用的话build时不会被打包,路径仍然为原来的路径 所以相对路径不会有效 如下: <Avatar style="margin ...
- C++入门经典-例5.5-空类型指针的使用
1:代码如下: // 5.5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...
- spring-ApplicationContext的事件传播(转)
ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的,通过ApplicationContext的publishE ...
- Could not parse configuration: /hibernate.cfg.xml
hibernate需要联网验证dtd,错误原因:未联网或网速不行
- Emacs 浏览网页
Emacs 浏览网页非常方便,还可以忽略掉那些烦人的小广告.特别是在看小说 的时候~ 在之前了解如何通过Emacs浏览网页时,大部分人使用的是w3m,折腾了半天,没能成功.后来无意间发现 ,有EWW ...
- BS架构和CS架构
B:browser 浏览器 S:server 服务器 C:client 客户端 BS:浏览器和服务器的关系,通过浏览器来访问服务器.比如:新浪.百度.等等. 优点:只要有浏 ...
- 【flask】使用pipenv管理依赖环境
[前堤] 已经安装了pipenv环境,并且项目目录下存在pipfile 如果要在另一个开发环境做开发,则将代码和Pipfile复制过去,运行以下命令: pipenv install --dev 是否要 ...
- flutter button
flutter button button类型: RaisedButton : 凸起的按钮,其实就是Android中的Material Design风格的Button ,继承自MaterialButt ...
- ControlTemplate in WPF —— Expander
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...