当前位置:网站首页>842. Arrange numbers
842. Arrange numbers
2022-07-28 22:39:00 【Hunter_ Kevin】
subject
Given an integer n, The digital 1∼n In a row , There will be many ways to arrange .
Now? , Please output all the arrangement methods in dictionary order .
Input format
All in one line , Contains an integer n.
Output format
Output all permutation schemes in dictionary order , One line for each scheme .
Data range
1≤n≤7
sample input :
3
sample output :
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Ideas
dfs+ to flash back
Code
#include <iostream>
using namespace std;
const int N = 10;
int n, path[N];// Store the currently inserted number
bool s[N];// Tag array , The tag subscript is i Has the number of been used
void dfs(int cur)
{
if(cur > n)// If n All positions have been inserted , here cur by n+1, Because array subscripts from 1 Start
{
for(int i = 1; i <= n; i++)printf("%d ",path[i]);
puts("");
return;
}
// Traverse all numbers , Find out the unused numbers
for(int i = 1; i <= n; i++)
{
if(!s[i])// If the first i Numbers not used
{
s[i] = true;// Mark the i Numbers have been used
path[cur] = i;// The first i Insert numbers into the current array of numbers
dfs(cur+1);// Continue to search deeply for the number that can be inserted into the next position
s[i] = false;// Restore the scene , take i Mark as unused
}
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
边栏推荐
- Analysis notes on let (const) temporary dead zone in JS
- 776. 字符串移位包含问题
- [Ruiji takeout project]day4 - dish management
- LVS+KeepAlived高可用部署实战应用
- Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
- There will be a black line on the border when the button in the wechat applet is clicked
- ssh免密登陆
- 20-09-27 the project is migrated to Alibaba toss record (the network card order makes the service unable to connect to DB through haproxy)
- internet的基本服务中文件传输命令是哪个
- CMD common commands
猜你喜欢

c语言实现字符串逆序排列

Att & CK Threat Intelligence

Lvs+keepalived high availability deployment practical application

Soft exam network engineer

静态成员static详解

105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)

Quadruped robot | gem (elevation map) + fast_ Deployment records of Leo (odometry) environment

Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)

JMeter installs third-party plug-ins plugins Manager
![[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login](/img/53/c578e0d1428ea569fb412a20019924.png)
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
随机推荐
Install PCL and VTK under the background of ROS installation, and solve VTK and PCL_ ROS conflict problem
Awk blank line filtering
Excel-vba quick start (XIII. Common usage of date)
SQL injection less34 (post wide byte injection + Boolean blind injection)
STM32 - systick timer (cubemx configures systick)
[leetcode] maximum depth of binary tree
DOM programming + events
Which is the file transfer command in the basic services of the Internet
770. 单词替换
Idea generate class diagram plug-in UML (super detailed)
[Ruiji takeout project]day4 - dish management
Command line agent: proxychains configuration
775. 倒排单词
Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
SSH password free login
Chrome encountered a problem when debugging the code. After modifying and saving the code in vscode, chrome did not update after refreshing
STM32 - interrupt overview (interrupt priority)
Sword finger offer II 065. The shortest word code (medium dictionary tree string array)
6K6w5LiA5qyh5pS75Ye75YiG5p6Q
internet的基本服务中文件传输命令是哪个
