当前位置:网站首页>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;
}
边栏推荐
- How much does it cost to develop a small program mall?
- [deep learning] image multi label classification task, Baidu paddleclas
- Static routing assignment of network reachable and telent connections
- Several methods of checking JS to judge empty objects
- ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
- Static comprehensive experiment
- Learning and using vscode
- Decrypt gd32 MCU product family, how to choose the development board?
- 通讯协议设计与实现
- 【统计学习方法】学习笔记——支持向量机(下)
猜你喜欢
【统计学习方法】学习笔记——支持向量机(下)
RHSA first day operation
[pytorch practice] write poetry with RNN
Session
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
【统计学习方法】学习笔记——提升方法
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
SQL head injection -- injection principle and essence
leetcode刷题:二叉树19(合并二叉树)
Polymorphism, final, etc
随机推荐
Processing strategy of message queue message loss and repeated message sending
Using stack to convert binary to decimal
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
Utiliser la pile pour convertir le binaire en décimal
[statistical learning methods] learning notes - Chapter 5: Decision Tree
Will the filing free server affect the ranking and weight of the website?
IPv6 experiment
What if does not match your user account appears when submitting the code?
Apache installation problem: configure: error: APR not found Please read the documentation
Cryptography series: detailed explanation of online certificate status protocol OCSP
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
"Series after reading" my God! It's so simple to understand throttling and anti shake~
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
广州市召开安全生产工作会议
【PyTorch实战】图像描述——让神经网络看图讲故事
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
leetcode刷题:二叉树20(二叉搜索树中的搜索)
Cookie
[learn micro services from 0] [02] move from single application to service
【从 0 开始学微服务】【03】初探微服务架构