当前位置:网站首页>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;
}
边栏推荐
- Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
- Establishment of Ruiji takeout development environment
- Command line agent: proxychains configuration
- Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
- fatal error: io. h: No such file or directory
- c语言实现字符串逆序排列
- Use webworker to perform background tasks
- 2022年一级建造师考试什么时候才能报名?
- 776. 字符串移位包含问题
- PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
猜你喜欢

Install PCL and VTK under the background of ROS installation, and solve VTK and PCL_ ROS conflict problem

PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)

JVM——自定义类加载器

Vscode ROS configuration GDB debugging error record
How do we do full link grayscale on the database?

imx6q gpio复用

flask之蓝图 补充openpyxl

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

Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator

842. 排列数字
随机推荐
ES6, deep copy, shallow copy
Excel-vba quick start (XIII. Common usage of date)
Mysql内置函数
STM32 board level support package for keys
Static details of static members
96. Different binary search trees (medium binary search tree dynamic planning)
Excel-VBA 快速上手(十三、日期的常见用法)
Wechat applet uses canvas drawing, round avatar, network background, text, dotted line, straight line
6K6w5LiA5qyh5pS75Ye75YiG5p6Q
6K6w5LiA5qyh5pS75Ye75YiG5p6Q
Win11 how to open software notification
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)
PC side web page special effects (offset series, obtain the coordinates of the mouse in the box, pop-up drag effect, magnifying glass effect)
[Ruiji takeout] day05 package management business development
Leetcode integer exercises integer inversion
Ngx+sql environment offline installation log (RPM installation)
Overall introduction of Ruiji takeout project
STM32 -- program startup process
ATT&CK 威胁情报
When can I sign up for the 2022 class I constructor examination?