当前位置:网站首页>全排列next_permutation()函数
全排列next_permutation()函数
2022-07-13 18:06:00 【偷完面具就瞎跑】
在头文件里面有如下代码:
int a[];
do
{
}
while(next_permutation(a,a+n));
产生1~n的全排列代码如下:
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n;
while(scanf("%d",&n)&&n){
int a[1000];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
do{
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}while(next_permutation(a,a+n));
}
return 0;
}
例如输入
3
1 0 2
如果有sort()
输出为
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0
若无
则输出为
1 0 2
1 2 0
2 0 1
2 1 0
发现函数next_permutation()是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序,在ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology - Alexandria, November 6th, 2015 A题就可以采用next_permutation()。
边栏推荐
猜你喜欢

一位年薪35W的测试被开除,回怼的一番话,令人沉思

Interface test and interface test automation

It is said that software testing can be done by everyone, but why are there still a large number of people who are discouraged every year?

VLAN and trunnk

自动备份MySQL。且保留7天案例

数制转换与子网划分

Why is it said that the testing post is a giant pit? The 10-year-old tester told you not to be fooled~

Linux上安装Redis

1、 Disk data recovery experiment report

Socket details
随机推荐
2个用例之间存在关联,怎么解?
Introduction to C language compiler
Principle and configuration of static routing
3、 Experimental report on the implementation of SMB sharing and FTP construction by freenas
Brief introduction to the simple seckill project
这应该是全网最完整的软件测试面试题了【速看】~
作为测试开发岗的面试官,我都是怎么选人的?
Thread pool and producer consumer model
Detailed explanation of sliding window
边缘计算 KubeEdge+EdgeMash
Redis主从集群搭建及哨兵模式配置
2021-11-13 attack and defense world test record 01misc
Volatile final explanation
How to solve the relationship between the two use cases?
RAID disk array
2021-11-7 bugku question making record 25 - Post
Flask基础入门七-Cookie和Session
字典树
TCP protocol details
[MySQL] paging query step pit