当前位置:网站首页>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;
}
边栏推荐
- Minimalist movie website
- leetcode刷题:二叉树23(二叉搜索树中的众数)
- [statistical learning method] learning notes - support vector machine (I)
- An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
- Utiliser la pile pour convertir le binaire en décimal
- visual stdio 2017关于opencv4.1的环境配置
- What is an esp/msr partition and how to create an esp/msr partition
- GCC compilation error
- [statistical learning method] learning notes - logistic regression and maximum entropy model
- 【统计学习方法】学习笔记——支持向量机(上)
猜你喜欢

Static vxlan configuration

leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
![[pytorch practice] image description -- let neural network read pictures and tell stories](/img/39/b2c61ae0668507f50426b01f2deee4.png)
[pytorch practice] image description -- let neural network read pictures and tell stories

opencv的四个函数

Preorder, inorder and postorder traversal of binary tree

Static comprehensive experiment

SQL head injection -- injection principle and essence

图形对象的创建与赋值

Vxlan 静态集中网关

visual stdio 2017关于opencv4.1的环境配置
随机推荐
leetcode刷题:二叉树23(二叉搜索树中的众数)
[statistical learning method] learning notes - logistic regression and maximum entropy model
数据库安全的重要性
What if does not match your user account appears when submitting the code?
Utiliser la pile pour convertir le binaire en décimal
Attack and defense world ----- summary of web knowledge points
利用栈来实现二进制转化为十进制
Typescript interface inheritance
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
GCC compilation error
idm服务器响应显示您没有权限下载解决教程
2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
(to be deleted later) yyds, paid academic resources, please keep a low profile!
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
NGUI-UILabel
Using stack to convert binary to decimal
Realize all, race, allsettled and any of the simple version of promise by yourself
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
On valuation model (II): PE index II - PE band