当前位置:网站首页>Informatics Aosai yibentong 1317: [example 5.2] combined output
Informatics Aosai yibentong 1317: [example 5.2] combined output
2022-04-22 08:20:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1317:【 example 5.2】 Combined output
【 Topic test site 】
1. Search for
【 Their thinking 】
solution 1: Search for
The difference between combination and arrangement is , A combination is a set of numbers , There is no order .
For permutations ,1 2 3 And 1 3 2 There are two permutations . For combinations ,1 2 3 And 1 3 2 It's the same combination .
If you search by the method of full ranking , The same number but in different order will occur many times , And we only need to count one of them .
In multiple permutations of the same number , Ascending order must be unique , Therefore, the ascending arrangement and combination must be one-to-one correspondence .( for example 3 2 1 The ascending order of the three numbers is 1 2 3), Here we will search for the ascending order output in various permutations , It can meet the requirements of “ Output each combination in ascending order ” The requirements of .
Make sure you find an ascending sequence , There are two specific ways to write
- How to write it 1: The number currently searched must be greater than or equal to the last number in the saved ascending order
- How to write it 2: The starting value of traversal is passed in during recursion
【 Solution code 】
solution 1: recursive
- How to write it 1: The current number is greater than or equal to the last number in ascending order
#include<bits/stdc++.h>
using namespace std;
bool vis[25];// Record which numbers have been used
int nums[25];// Record which numbers to output , Recorded in the num[1],num[2]...,num[r]
int n, r;// from n Out of these elements r Elements
void dfs(int p)//p: To determine the number in ascending order
{
for(int i = 1; i <= n; ++i)
{
if(vis[i] == false && i > nums[p-1])//p For the initial value 1 when , Fetch num[0], Its value is 0, It is known that i Is greater than 0, No impact on operation
{
nums[p] = i;// Add numbers
vis[i] = true;// Numbers i Used
if(p == r)// If it has been filled r A digital
{
// The output exists num Number of permutations in
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;
}
- How to write it 2: The starting value of traversal is passed in during recursion
#include<bits/stdc++.h>
using namespace std;
bool vis[25];// Record which numbers have been used
int nums[25];// Record which numbers to output , Recorded in the num[1],num[2]...,num[r]
int n, r;// from n Out of these elements r Elements
void dfs(int p, int st)//p: To determine the number in ascending order , The number to be filled in is greater than or equal to st
{
for(int i = st; i <= n; ++i)
{
if(vis[i] == false)
{
nums[p] = i;// Add numbers
vis[i] = true;// Numbers i Used
if(p == r)// If it has been filled r A digital
{
// The output exists num Number of permutations in
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;
}
版权声明
本文为[Jun Yi_ noip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220704294268.html
边栏推荐
- 牛客白月赛6【题解】
- 每个人都应该使用 OKR吗?
- MySQL 进阶 视图 -- 视图介绍、视图CRUD语法、检查选项(CASCADED、LOCAL)、视图的更新、视图作用、视图案例
- Redis非关系型数据库—Redis高可用、持久化及性能管理
- JDBC configuration method of JMeter
- 实验二、数据科学中的数学基础
- 计算机保研面试中,都有哪些令人窒息的问题?
- 深度确定性策略梯度(DDPG)
- 铝酸镧LaAlO3晶体基片|硅Si晶体基片|掺铁钛酸锶Fe:SrTiO3晶体基片|掺铌钛酸锶Nb:SrTiO3晶体基片|齐岳供应晶体基片种类丰富
- LeetCode_118. 杨辉三角_动态规划_int**的学习
猜你喜欢
随机推荐
Cherno_游戏引擎系列教程(5):101~
The integrated optical access equipment provides 16 E1 service ports, 4 100m isolated networks and 2 Gigabit isolated Ethernet optical terminals
pytest_ First class
Tensorflow usage notes
小公司不会发正式 offer
PDH光端机综合多业务光接入设备双光口传16路E1 2M+4路千兆1000M网络+4路百兆共享网络机架式双电源
Hanyuan hi tech PDH optical transceiver double optical port protection + 4-way E1 + 4-way Gigabit Network + 4-way 100m network optical transceiver
OneNET连接流程
Cr doped strontium titanate Cr: SrTiO3 crystal substrate | NaCl < 111 > 10x10x2 0mm1sp crystal substrate | Al2O3 sapphire crystal substrate | Qiyue biology
Sun Yuchen, founder of wave field Tron, announced that it will officially launch the decentralized stable currency usdd
Copula函数初了解
LabVIEW 2012中的收藏选板导入到LabVIEW 2013
牛客白月赛6【题解】
Architects evaluate the current situation and development prospect of software industry
RSYNC及inotify远程同步
实验二、数据科学中的数学基础
Q-Learning & Sarsa
提供4路E1业务口4路百兆隔离网络2M E1专网多业务PDH光端机
电信级双光口保护8E1+4路物理隔离千兆网络光端机1000M网络100M光端机
OpenFeign之响应处理




![[quick link] a necessary calculation tool for Electronic Engineers - square wave circuit aided design form](/img/64/7c857ed94047f2a7b1d7be6d35e3b7.png)



