当前位置:网站首页>全排列(DFS和next_permutation解法)
全排列(DFS和next_permutation解法)
2022-04-23 01:21:00 【OldLeft】
从 n 个不同元素中任取m (m≤n) 个元素,按照一定的顺序排列起来,叫做从 n 个不同元素中取出 m 个元素的一个排列。当 m=n 时所有的排列情况叫全排列。
今天这道题目很简单就是给你一个整数 n ,计算 [1,n] 所有数字的排列组合。
输入格式
第一行输入一个整数n(1≤n≤10).
输出格式
第一行输出一个全排列的方案总数 m。
接下来按照字典序依次输出这 m 个排列。
排列的字典的比较方法和字符串一样,比如两个排列 a,b,从第一个数开始,找到第一个不同的数的位置为i,然后比较 a[i] 和b[i] 的大小,如果 a[i] 小的,那么 a 的字典序就小,否则 b 的字典序更小。
样例输入1
2
样例输出1
2
12
21
样例输入2
3
样例输出2
6
123
132
213
231
312
321
解法1: 直接用stl里的next_permutation(a,a+n)

#include<bits/stdc++.h>
using namespace std;
int n,a[20];
int main(){
cin>>n;
for(int i=0;i<n;i++){
a[i]=i+1;
}
int cnt=0;
do{
cnt++;
}while(next_permutation(a,a+n));
cout<<cnt<<endl;
do{
for(int i=0;i<n;i++){
cout<<a[i];
}
cout<<endl;
}while(next_permutation(a,a+n));
return 0;
}
解法2: DFS
简单DFS题目,直接搜索就可以了。
每次从 1 - n1−n 选择一个前面没有使用过的数字,把前面使用过的数字进行标记。使用结束的时候要把前面标记过的数字进行释放。
这个时候你会发现输出的答案正好是按照字典序输出。其实也是在意料中,因为我们每次尝试的时候都是从最小的开始。
注意这里不能加上重复性剪枝,因为这里是排列,和搜索顺序有关。
#include<bits/stdc++.h>
using namespace std;
bool vis[20];
int n,m=1;
void dfs(int x,int y){
if(x==n){
printf("%d\n",y);
return;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
vis[i]=true;
dfs(x+1,y*10+i);
vis[i]=false;
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
m*=i;
}
printf("%d\n",m);
dfs(0,0);
}
版权声明
本文为[OldLeft]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43833610/article/details/115772197
边栏推荐
- Small example of gin - get request 1-handle handles get requests
- Redis forgets the password and resets the password
- Analysis and configuration process of epit periodic timer for imx6ull bare metal development
- GBase 8t索引
- Zhang Jian of Huawei cloud IOT expert group: he has become a senior engineer of Huawei since he was 22. The code is what I want to say to the world
- GBase 8s 备份介绍
- Yyds dry goods counting flag variable rule
- IMX6ULL裸机开发之EPIT周期性定时器分析及配置过程
- 【Android工程师与智能家居产品的第一次接触③】SmartConfig一键配网在硬件端的具体实现|ESP8266一键配网在Arduino的具体实现|玉念聿辉
- How to introduce SPI into a project
猜你喜欢

Basic operation of Android local database | multi thread operation database | addition, deletion, modification and query of database | batch insertion into database | basic use of thread pool | Yu nia

Text justify, orientation, combine text attributes

Jijian cloud x servicego: help hardware manufacturers realize intelligent management of equipment repair and maintenance

Real time monitoring and management of distribution circuit power consumption of acrel-2000 power monitoring system in xingqingfang Xinxing square distribution substation

Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)

Is it difficult for girls to learn software testing?

Alibaba cloud container & Service Grid product technology trends (202203)

In the second half of the smart watch, opportunities and challenges coexist

10月24号是什么?中国的高级程序员为什么没有五六十岁的,而国外人、、、,玉念聿辉带你过1024程序员节日

IMX6ULL裸機開發之硬件IIC分析及配置過程
随机推荐
Good test data management, in the end how to do?
[actf2020 freshman competition]
What kind of project is suitable for automated testing?
Learning of gin framework -- golang
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
Vs + C realizes that the form input box displays gray text by default
Yunrong technology joined the dragon dragon dragon community to help the digital transformation of the financial industry
VS+C# 实现窗体输入框默认显示灰色文字
Articles for May
安全用电管理平台在靖边博物馆安全用电管理系统的应用
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
Interview eight part essay (disorderly order, no classification)
JD side: comment un thread enfant obtient - il la valeur de threadlocal du thread parent? Je suis couvert...
那些咸鱼上买来的代码|ssm酒店客房管理系统|买来的源码是否真的可以使用|来自程序员的困惑|玉念聿辉|大丑村吴明辉
Gbase 8s存储结构简介及空间管理
Examples of branch and loop statements
Thingskit Internet of things platform
Basic knowledge of software testing (detailed version) collection of this article is enough
Why should I object to DBA's participation in business (issuing reports / changing data)
The more "intelligent" the machine is, the easier it is for data taggers to be eliminated? Manfu Technology