当前位置:网站首页>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;
}
边栏推荐
- [Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
- Qt+ffmpeg environment construction
- LVS+KeepAlived高可用部署实战应用
- flask之蓝图 补充openpyxl
- 842. 排列数字
- 770. 单词替换
- DOM programming + events
- 105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)
- 使用PCL批量显示PCD点云数据流
- 76. Minimum coverage substring (hard sliding window hash table string)
猜你喜欢

6K6w5LiA5qyh5pS75Ye75YiG5p6Q

Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)

STM32 - reset and clock control (cubemx for clock configuration)

Concise history of graphic technology
![[Ruiji takeout project]day4 - dish management](/img/2a/2d9deb7a583aa37b38a67ef2c74ee7.png)
[Ruiji takeout project]day4 - dish management

flask之蓝图 补充openpyxl

When can I sign up for the 2022 class I constructor examination?

Ruiji takeout - background login function development

Changes in the history of oscilloscope development

ATT&CK初步了解
随机推荐
Concise history of graphic technology
75. Color classification (medium array double pointer sorting)
imx6q gpio复用
flask之蓝图 补充openpyxl
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
SQL injection less42 (post stack injection)
Use REM to make the font size adaptive to the screen
Att & CK Threat Intelligence
[connect set-top box] - use ADB command line to connect ec6108v9 Huawei Yuehe box wirelessly
Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator
Excel-vba quick start (XIII. Common usage of date)
Detection and tracking evaluation index
Imx6q GPIO multiplexing
elment-plus图标input上面带的图标为什么不显示
Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
Sword finger offer II 066. sum of words (medium prefix tree design string)
What is time complexity
Integrating database Ecology: using eventbridge to build CDC applications
Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
Awk blank line filtering
