当前位置:网站首页>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
边栏推荐
- App uses the template message from WeChat official account for message push.
- 那些咸鱼上买来的代码|ssm酒店客房管理系统|买来的源码是否真的可以使用|来自程序员的困惑|玉念聿辉|大丑村吴明辉
- 世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
- VRF in Mina
- Introduction to gbase 8s shared memory buffer pool
- Introduction à la structure de stockage de gbase 8S et à la gestion de l'espace
- Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
- 8GBs communication between client and server
- (product resources) mingdeyang ad8488 module high performance digital X-ray FMC interface 128 analog channel high-speed ADC chip
- GBase8s SQL 引擎框架简介
猜你喜欢

无关联进程间通信 —— 命名管道的创建和使用

gin框架的学习--golang

计蒜客:数独(DFS)

Soatest preliminary understanding

JD side: comment un thread enfant obtient - il la valeur de threadlocal du thread parent? Je suis couvert...

Detailed explanation of Milvus 2.0 quality assurance system

京东一面:子线程如何获取父线程 ThreadLocal 的值?我蒙了。。。

Three technical solutions of ant group were selected as "typical solutions for information technology application and innovation in 2021"

京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。

王子救公主(DFS)
随机推荐
京東一面:子線程如何獲取父線程 ThreadLocal 的值?我蒙了。。。
App中使用微信公众号的模版消息来进行消息推送
Software maintenance exercises
GBASE 8s并发控制之封锁类型
Gbase 8s query processing and optimization
Gbase 8s 并发控制之粒度锁介绍
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
Learning of gin framework -- golang
天梯赛L2-6 树的遍历
Gbase 8s客户端与服务器的通信
Configuration of imx6ull bare metal development analysis and configuration process of elcdif lighting RGB LCD
Gbase 8s检查点简介
Level 4 city area table xlsx, SQL file, domestic, provincial, county, street, township level 4 address in China (name, linkage ID, level, end level or not (1-yes))
Slow response of analysis API
GBASE 8s 共享内存缓冲池介绍
What is October 24th? Why are there no senior programmers in China in their fifties and sixties, while foreigners,,, Yu Nianyu Hui take you to celebrate the 1024 programmer Festival
The origin explanation and use example of image pre training model
王子救公主(DFS)
Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)