当前位置:网站首页>递归实现排列型枚举(DAY 93)
递归实现排列型枚举(DAY 93)
2022-08-02 04:22:00 【张学恒】
1:题目
把 1∼n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序。
输入格式
一个整数 n。
输出格式
按照从小到大的顺序输出所有方案,每行 1 个。
首先,同一行相邻两个数用一个空格隔开。
其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面。
数据范围
1≤n≤9
输入样例:
3
输出样例:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
2:代码实现
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
vector<int> path;
void dfs(int u, int state)
{
if (u == n)
{
for (auto x : path) cout << x << ' ';
cout << endl;
return;
}
for (int i = 0; i < n; i ++ )
if (!(state >> i & 1))
{
path.push_back(i + 1);
dfs(u + 1, state + (1 << i));
path.pop_back();
}
}
int main()
{
cin >> n;
dfs(0, 0);
return 0;
}
边栏推荐
猜你喜欢

What if some fields don't want to be serialized?

数学建模学习(76):多目标线性规划模型(理想法、线性加权法、最大最小法),模型敏感性分析

吴恩达机器学习系列课程笔记——第八章:神经网络:表述(Neural Networks: Representation)

一次跳出最外层循环

ADSP21489仿真:Failed to set breakpoint: Can‘t set breakpoints in the current state: Running

How to save a section of pages in a PDF as a new PDF file

The line chart with square PyQt5_pyqtgraph mouse

JDBC再回顾
![[Win11] PowerShell cannot activate Conda virtual environment](/img/53/464ffb5ef80ce8f6ee19e9ea96c159.png)
[Win11] PowerShell cannot activate Conda virtual environment

Deep Blue Academy - 14 Lectures on Visual SLAM - Chapter 7 Homework
随机推荐
internship:数据库表和建立的实体类及对应的枚举类之间的联系示例
批量--10---根据set数拆分文件
Deep Blue Academy - Visual SLAM Lecture Fourteen - Chapter 5 Homework
Learn about the sequential storage structure of binary tree - heap
Deep Learning Basics Overfitting, Underfitting Problems, and Regularization
数学建模学习(76):多目标线性规划模型(理想法、线性加权法、最大最小法),模型敏感性分析
立方体卫星Light-1
(一)代码输出题 —— reverse
ClickHouse的客户端命令行参数
STM32 OLED显示屏
C# Thread IsBackground作用
数据复制系统设计(3)-配置新的从节点及故障切换
STM32 OLED显示屏--SPI通信知识汇总
nr部分计算
如果有些字段不想进行序列化怎么办?
Scala基础【常用方法补充、模式匹配】
多主复制下处理写冲突(1)-同步与异步冲突检测及避免冲突
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
互动投影墙深受展览展示喜爱的原因分析
Deep Blue Academy - 14 Lectures on Visual SLAM - Chapter 7 Homework