当前位置:网站首页>递归实现排列型枚举(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;
}
边栏推荐
猜你喜欢
Learn about the sequential storage structure of binary tree - heap
关于地图GIS开发事项的一次实践整理(上)
批量--10---根据set数拆分文件
What if some fields don't want to be serialized?
A practice arrangement about map GIS (below) GIS practice of Redis
ADSP21489数据手册表摘要
Go 语言是如何实现切片扩容的?【slice】
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
AFMG SysTune1.3.7使用图解
PyQt5_pyqtgraph鼠标在折线图上画方形
随机推荐
Nuscenes数据集总结(下)
【C语言程序】求直角三角形边长
已更新 联通 电信 tiny模式
Visual SLAM Lecture Fourteen - Lecture 13 Practice: Designing a SLAM system (the most detailed code debugging and running steps)
多主复制下处理写冲突(1)-同步与异步冲突检测及避免冲突
压缩包密码如何快速删除?
7亿听众背后的在线音频掘金故事
使用GD32F207的高级定时器来产生PWM波出现的隐藏BUG
jetracer_pro_2GB AI Kit system installation instructions
ADSP21489工程中LDF文件配置详解
Sentinel熔断之非控制台方式总结
MySQL read-write separation mysql-proxy deployment
ClickHouse的客户端命令行参数
数据复制系统设计(3)-配置新的从节点及故障切换
轮询和长轮询的区别
【STM32】ADC采集光敏数据(不看库函数手册进行配置)
多数据中心操作和检测并发写入
How to save a section of pages in a PDF as a new PDF file
(一)代码输出题 —— reverse
违约金过高”的认定依据