当前位置:网站首页>HZOJ #235. 递归实现指数型枚举
HZOJ #235. 递归实现指数型枚举
2022-07-07 10:36:00 【段舸】
题目:235. 递归实现指数型枚举
题目传送门:235题
题目描述
从 1−n1−n 这 nn 个整数中随机选取任意多个,每种方案里的数从小到大排列,按字典序输出所有可能的选择方案。
输入
输入一个整数 nn。(1≤n≤10)(1≤n≤10)
输出
每行一组方案,每组方案中两个数之间用空格分隔。
注意每行最后一个数后没有空格。
输入样例
3
样例输出
1
1 2
1 2 3
1 3
2
2 3
3
样例输入
4
样例输出
1
1 2
1 2 3
1 2 3 4
1 2 4
1 3
1 3 4
1 4
2
2 3
2 3 4
2 4
3
3 4
4
代码1
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int n, num[15];
void print(int ind)
{
for (int i = 0; i <= ind; i++)
{
if (i) cout << " ";
cout << num[i];
}
cout << endl;
}
void func(int start, int ind)
{
for (int i = start; i <= n; i++)
{
num[ind] = i;
print(ind);
func(i + 1, ind + 1);
}
}
int main()
{
cin >> n;
func(1, 0);
return 0;
}
代码2
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int n, num[15],cnt;
void print()
{
for (int i = 0; i <= cnt; i++)
{
if (i) cout << " ";
cout << num[i];
}
cout << endl;
}
void func(int start)
{
for (int i = start; i <= n; i++)
{
num[cnt] = i;
print();
cnt++;
func(i + 1);
cnt--;
}
}
int main()
{
cin >> n;
func(1);
return 0;
}
边栏推荐
- 广州市召开安全生产工作会议
- Zhimei creative website exercise
- ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
- Vxlan static centralized gateway
- The IDM server response shows that you do not have permission to download the solution tutorial
- VSCode的学习使用
- (待会删)yyds,付费搞来的学术资源,请低调使用!
- 30. Feed shot named entity recognition with self describing networks reading notes
- 跨域问题解决方案
- "Series after reading" my God! It's so simple to understand throttling and anti shake~
猜你喜欢

Attack and defense world ----- summary of web knowledge points

SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)

Epp+dis learning road (2) -- blink! twinkle!

Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
![[statistical learning methods] learning notes - improvement methods](/img/c5/515f171995da8e424de290228b54f8.png)
[statistical learning methods] learning notes - improvement methods

leetcode刷题:二叉树20(二叉搜索树中的搜索)

Preorder, inorder and postorder traversal of binary tree

leetcode刷题:二叉树23(二叉搜索树中的众数)
![Routing strategy of multi-point republication [Huawei]](/img/5c/2e3b739ce7199f0d2a4ddd7c3856fc.jpg)
Routing strategy of multi-point republication [Huawei]

leetcode刷题:二叉树21(验证二叉搜索树)
随机推荐
【统计学习方法】学习笔记——第五章:决策树
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
Common knowledge of one-dimensional array and two-dimensional array
gcc 编译报错
leetcode刷题:二叉树20(二叉搜索树中的搜索)
visual stdio 2017关于opencv4.1的环境配置
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
Zhimei creative website exercise
[statistical learning methods] learning notes - Chapter 4: naive Bayesian method
SQL Lab (46~53) (continuous update later) order by injection
浅谈估值模型 (二): PE指标II——PE Band
VSCode的学习使用
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
Charles: four ways to modify the input parameters or return results of the interface
Decrypt gd32 MCU product family, how to choose the development board?
RHSA first day operation
leetcode刷题:二叉树23(二叉搜索树中的众数)
Learning and using vscode
【统计学习方法】学习笔记——提升方法
Configure an encrypted web server