当前位置:网站首页>信息学奥赛一本通 1317:【例5.2】组合的输出
信息学奥赛一本通 1317:【例5.2】组合的输出
2022-04-22 07:04:00 【君义_noip】
【题目链接】
【题目考点】
1. 搜索
【解题思路】
解法1:搜索
组合与排列的区别为,组合是一个数字集合,是没有顺序的。
对于排列来说,1 2 3与1 3 2是两种排列。对于组合来说,1 2 3与1 3 2是同一种组合。
如果按搜索全排列的方法来进行搜索,数字相同但顺序不同的情况会多次出现,而我们只需要统计其中的一次。
在相同数字的多种排列中,升序排列一定是唯一的,因而升序排列与组合一定是一一对应的。(例如3 2 1三个数字的升序排列为1 2 3),这里我们就搜索多种排列中的升序排列输出,即可满足题目中“将每个组合按升序顺序输出”的要求。
要保证搜索到的是升序序列,具体写法有两种
- 写法1:当前搜索到的数字要大于等于已经保存的升序排列的最后一个数字
- 写法2:递归时传入遍历的起始值
【题解代码】
解法1:递归
- 写法1:当前数字大于等于升序排列的最后一个数字
#include<bits/stdc++.h>
using namespace std;
bool vis[25];//记录哪些数已经用过
int nums[25];//记录要输出哪些数,记录在num[1],num[2]...,num[r]
int n, r;//从n个元素中抽出r个元素
void dfs(int p)//p:要确定升序排列中的第几个数
{
for(int i = 1; i <= n; ++i)
{
if(vis[i] == false && i > nums[p-1])//p为初始值1时,取到num[0],其值为0,已知i都大于0,不影响运行
{
nums[p] = i;//添加数字
vis[i] = true;//数字i已被使用
if(p == r)//如果已经填充了r个数字
{
//输出存在num中的排列数
for(int j = 1; j <= r; ++j)
cout << setw(3) << nums[j];
cout << endl;
}
else
dfs(p+1);
vis[i] = false;
}
}
}
int main()
{
cin >> n >> r;
dfs(1);
return 0;
}
- 写法2:递归时传入遍历的起始值
#include<bits/stdc++.h>
using namespace std;
bool vis[25];//记录哪些数已经用过
int nums[25];//记录要输出哪些数,记录在num[1],num[2]...,num[r]
int n, r;//从n个元素中抽出r个元素
void dfs(int p, int st)//p:要确定升序排列中的第几个数,要填入的数字大于等于st
{
for(int i = st; i <= n; ++i)
{
if(vis[i] == false)
{
nums[p] = i;//添加数字
vis[i] = true;//数字i已被使用
if(p == r)//如果已经填充了r个数字
{
//输出存在num中的排列数
for(int j = 1; j <= r; ++j)
cout << setw(3) << nums[j];
cout << endl;
}
else
dfs(p+1, i);
vis[i] = false;
}
}
}
int main()
{
cin >> n >> r;
dfs(1, 1);
return 0;
}
版权声明
本文为[君义_noip]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lq1990717/article/details/124335973
边栏推荐
- Product test with payment function
- HLS / Chisel 利用CORDIC双曲系统实现平方根计算
- Hanyuan hi tech PDH optical transceiver double optical port protection + 4-way E1 + 4-way Gigabit Network + 4-way 100m network optical transceiver
- Double optical port protection 8e1 + 8-way physical isolation 100M Ethernet optical transceiver PDH optical transceiver
- 【速领】电子工程师入门必备计算工具-方波电路辅助设计表格
- HLS / Chisel 实践CORDIC高性能计算复数平方根
- Wechat applet page routing
- LDAP user login authentication verification and query
- JDBC configuration method of JMeter
- FileNotFoundError: [Errno 2] No such file or directory
猜你喜欢

tf.keras.layers.TimeDistributed函数

牛客白月赛6【题解】

pycharm终端pip安装Error: “pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称

Double optical port protection 8e1 + 8-way physical isolation 100M Ethernet optical transceiver PDH optical transceiver

2022年全国中职组网络安全国赛赛题思路(仅自己一个做题的思路)——网络安全竞赛试题(7)

MySQL 进阶 视图 -- 视图介绍、视图CRUD语法、检查选项(CASCADED、LOCAL)、视图的更新、视图作用、视图案例

金九银十面试季,字节跳动面试题拿走不谢(附详细答案解析)

ER 和 EER 模型

宝塔面板设置django纯接口和channel及mysql遇到的一些问题记录

实验二、数据科学中的数学基础
随机推荐
UI automatic login bypass verification code
氧化镁MgO晶体基片|钛酸锶SrTiO3晶体基片|铌酸锂LiNbO3晶体基片;直径10mm
Architects evaluate the current situation and development prospect of software industry
VS Code 设置换行
JMETER的JDBC配置方法
MATLAB中的小技巧
MYSQL04_算术、逻辑、位、运算符、运算符对应的习题
架构师评价当前软件行业现状及发展前景
OpenFeign超时设置
2022.成都90分钟不限次工作室如何使用
软件测试之接口自动化面试题汇总
二叉树
ADB logcat log
金九银十面试季,字节跳动面试题拿走不谢(附详细答案解析)
laravel8-使用jwt
Optical fiber transmission 16 CHANNEL E1 + 4-channel Gigabit isolated Ethernet optical transceiver 2m private network Gigabit Ethernet integrated multi service PDH optical transceiver
牛客白月赛5 【题解 数学场】
综合业务PDH光端机4路千兆隔离以太网络+4路E1专网业务2M综合业务光端机
Hanyuan hi tech 8-way multi service PDH optical transceiver double optical port protection + 8-way E1 + 4-way Gigabit Ethernet + 4-way 100m network electric port
php 使用redis简单实例