当前位置:网站首页>Full Permutation (DFS and next_permutation solution)
Full Permutation (DFS and next_permutation solution)
2022-04-23 01:25:00 【OldLeft】
from n Any of the different elements m (m≤n) Elements , Put them in a certain order , It's called from n Take out... Of the different elements m An arrangement of elements . When m=n All permutations are called full permutations .
Today's topic is very simple, that is to give you an integer n , Calculation [1,n] The arrangement and combination of all numbers .
Input format
Enter an integer in the first line n(1≤n≤10).
Output format
The first row outputs the total number of schemes in a full permutation m.
Next, output these in dictionary order m Permutation .
The comparison method of arranged dictionaries is the same as that of strings , For example, two permutations a,b, Start with the first number , The position where the first different number is found is i, Then compare a[i] and b[i] Size , If a[i] Small , that a The dictionary order is small , otherwise b The dictionary order is smaller .
The sample input 1
2
Sample output 1
2
12
21
The sample input 2
3
Sample output 2
6
123
132
213
231
312
321
solution 1: Direct use stl Inside 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;
}
solution 2: DFS
Simple DFS subject , Just search directly .
Each time from 1 - n1−n Choose a number that has not been used before , Mark the previously used numbers . At the end of use, release the previously marked number .
At this time, you will find that the output answer is exactly in dictionary order . In fact, it is also expected , Because every time we try, we start from the smallest .
Note that repeated pruning cannot be added here , Because this is an arrangement , Related to the search order .
#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://yzsam.com/2022/04/202204230120462249.html
边栏推荐
- 2n皇后问题
- Gbase 8s Group by 功能介绍
- Earth day collection: Microsoft and Intel invite you to get the green Ambassador badge and give you negative carbon emission!
- Cloud native Virtualization: building edge computing instances based on kubevirt
- In the context of Internet plus, how can enterprises innovate customer service?
- GBase 8s 备份介绍
- API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
- Gbase 8s shared memory segment deletion
- 那些咸鱼上买来的代码|ssm酒店客房管理系统|买来的源码是否真的可以使用|来自程序员的困惑|玉念聿辉|大丑村吴明辉
- Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)
猜你喜欢

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

From thinking to practice, digital transformation is the successful path of it operation

Android本地数据库基础操作|多线程操作数据库|数据库的增删改查|批量插入数据库|线程池基础使用|玉念聿辉

ai2022新功能,illustrator 2022 新功能介绍

计蒜客:方程的解数

Yyds dry goods counting flag variable rule

Huawei CDN is fast everywhere

Google developer tool preserve log

Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!

gin框架的学习--golang
随机推荐
gin框架的学习--golang
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Cloud native Virtualization: building edge computing instances based on kubevirt
Introduction and management of gbase 8s database log
Is it difficult for girls to learn software testing?
GBASE 8s 并发控制之封锁操作
Innovative practice of short video content understanding and generation technology in meituan
Blocking type of gbase 8s concurrency control
Update description of the latest process engine flowable 6.7.2
Five commonly used order receiving platforms recommended by programmers
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
Interface automation session authentication solution
最新流程引擎 flowable 6.7.2 更新说明
Unrelated interprocess communication -- creation and use of named pipes
Gbase 8s存儲結構簡介及空間管理
iTextSharp 显示中文字体
Practice and exploration of knowledge map visualization technology in meituan
VS+C# 实现窗体输入框默认显示灰色文字
Itextsharp page setup
Learning of gin framework -- golang