当前位置:网站首页>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;
}
边栏推荐
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- 【统计学习方法】学习笔记——第四章:朴素贝叶斯法
- Configure an encrypted web server
- Customize the web service configuration file
- Is it safe to open an account in Ping An Securities mobile bank?
- Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- 【从 0 开始学微服务】【01】什么是微服务
- gcc 编译报错
猜你喜欢

leetcode刷题:二叉树24(二叉树的最近公共祖先)

"Series after reading" my God! It's so simple to understand throttling and anti shake~

idm服务器响应显示您没有权限下载解决教程

H3C HCl MPLS layer 2 dedicated line experiment

Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment

SQL Lab (46~53) (continuous update later) order by injection

解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually

ES底层原理之倒排索引

【统计学习方法】学习笔记——支持向量机(下)

Processing strategy of message queue message loss and repeated message sending
随机推荐
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
leetcode刷题:二叉树24(二叉树的最近公共祖先)
Using stack to convert binary to decimal
Tutorial on principles and applications of database system (009) -- conceptual model and data model
On valuation model (II): PE index II - PE band
Utiliser la pile pour convertir le binaire en décimal
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
AirServer自动接收多画面投屏或者跨设备投屏
Day-15 common APIs and exception mechanisms
Customize the web service configuration file
IPv6 experiment
Epp+dis learning road (2) -- blink! twinkle!
【从 0 开始学微服务】【01】什么是微服务
Day-18 hash table, generic
Day-19 IO stream
SQL Lab (46~53) (continuous update later) order by injection
[pytorch practice] use pytorch to realize image style migration based on neural network
Static comprehensive experiment
Inverted index of ES underlying principle