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库里,作用是传入一个数组,输出这个数 ...
随机推荐
- D. Treasure Hunting ( 思维题 , 贪心)
传送门 题意: 在一个 n * m 的地图里,有 k 个宝藏,你的起点在 (1, 1), 每次你能 向下向右向左移动(只要在地图里): 现在,有 q 个安全的列, 你只有在这些列上面,你才能 ...
- [HG]提高组 题解
首先很容易想到暴力DP 设状态f[i][j]表示当前放了第i个数,最大的数为j的方案数. 然后根据转移推出实际上是在下图走路的方案数 \[ \left( \left( \begin{matrix} x ...
- idea中JSP页面不能访问静态资源(图片,js,css)
必须配置SpringMvc对访问静态资源的支持,idea默认就是在main/webapp 下的文件路径,要在web-info同级的resource文件下放置,JSP中 ${pageContext.re ...
- 9.Python关键字(保留字)一览表
保留字是 Python 语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量.函数.类.模板以及其他对象命名. Python 包含的保留字可以执行如下命令进 ...
- phpfor函数和foreach函数
PHP for 循环 PHP While 循环 PHP 函数 PHP for 循环执行代码块指定的次数. PHP for 循环 如果您已经提前确定脚本运行的次数,可以使用 for 循环. 语法 for ...
- springboot2.x 整合redis集群的几种方式
一.不指定redis连接池 #系统默认连接池 yml配置文件: spring: redis: cluster: nodes: - 192.168.1.236:7001 - 192.168.1.236: ...
- sshd_config已修改,ssh还是无法远程问题解决
环境:vmware下的debian10.2 /etc/sshd/sshd_config 配置文件已经完成修改开启允许root账号登录配置 ssh远程返回如下信息: [Administrator.NIH ...
- flutter 隐藏返回按钮 自定义返回按钮
自定义返回按钮 //改变颜色 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( leading: BackBu ...
- Python_List对象内置方法详解
目录 目录 前言 软件环境 列表List 修改列表的元素 插入列表元素 extend 将序列中的元素迭代的附加到list中 insert 在指定的索引号中插入一个元素 删除列表元素 del 删除Lis ...
- apache禁止指定的user_agent访问
user_agent:也就是浏览器标识#禁止指定user_agent <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTT ...