当前位置:网站首页>AcWing 92. 递归实现指数型枚举
AcWing 92. 递归实现指数型枚举
2022-07-24 12:03:00 【WDLieqi】
从 1∼n 这 n 个整数中随机选取任意多个,输出所有可能的选择方案。
输入格式
输入一个整数 n。
输出格式
每行输出一种方案。
同一行内的数必须升序排列,相邻两个数用恰好 1 个空格隔开。
对于没有选任何数的方案,输出空行。
本题有自定义校验器(SPJ),各行(不同方案)之间的顺序任意。
数据范围
1 ≤ n ≤ 15
输入样例:
3
输出样例:
//空行
3
2
2 3
1
1 3
1 2
1 2 3
思路
我们可以把每一个节点分为选和不选,最后走一遍即可。
#include <iostream>
using namespace std;
const int N = 20;
int n;
bool vis[N]; //判断选还是不选
void dfs(int u) //第几层就是筛选第几个数字
{
if (u > n) //不可以有等号,如果有等号会少一层递归,即最后一层无法递归
{
for (int i = 1; i <= n; i ++) //从1到n选择
if (vis[i]) cout << i << ' ';
cout << '\n';
return;
}
else
{
vis[u] = true; //选这个数字
dfs(u + 1);
vis[u] = false; //不选这个数字
dfs(u + 1);
}
}
int main()
{
cin >> n;
dfs(1); //从1开始选择,到n结束,所以不能从0开始;
return 0;
}
边栏推荐
- L1-059 敲笨钟
- 哈希——349. 两个数组的交集
- Hash - 18. Sum of four numbers
- Experience of redis deepwater area -- Interview reference
- Leetcode:51. queen n
- Makefile quick use
- [markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
- gcc -l参数和-L参数的区别
- 动态内存管理
- [I also want to brush through leetcode] 468. Verify the IP address
猜你喜欢

3、 Implementation principle of MFC message mapping mechanism

使用Prometheus+Grafana实时监控服务器性能

Three small knowledge points about data product managers

String -- 344. Reverse string

Introduction to Devops and common Devops tools

Two important laws about parallelism

6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!

Semaphore details

Linked list - 142. Ring linked list II

Jmeter-While控制器
随机推荐
Grep actually uses ps/netstat/sort
链表——剑指offer面试题 02.07. 链表相交
Hash - 15. Sum of three numbers
生信周刊第37期
The difference between synchronized and lock locks
哈希——1. 两数之和——有人白天相爱,有人夜里看海,有人力扣第一题都做不出来
Common shortcuts to VIM editor
基于ARM和FPGA的数字示波器设计——QMJ
Hash - 202. Happy number
[mathematical basis of Cyberspace Security Chapter 9] finite field
使用Prometheus+Grafana实时监控服务器性能
String - Sword finger offer 05. replace spaces
Use of multithreading in QT
L1-049 seat allocation of ladder race
JVM visualvm: multi hop fault handling tool
makefile快速使用
源码分析Sentry用户行为记录实现过程
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
20000 words detailed explanation, thoroughly understand es!
三、MFC消息映射机制实现原理