版本比较
搜索
正文
增加 49 行删除 49 行
12345678912345678910111112131415161617181819202121222324254 行未改动30313233343536373839404142343536373839404142434445464748495051525253545556575859606061626263646465666768696970717172737475767677787980818282838485864 行未改动919293949596979899100959697989910010110210210310410410510610710810911011111111211311411511 行未改动12712812913013113213313413513613113213313413513613713813813914014014114214314414514514614714714814915015115 行未改动# 搜索 ## - 枚举元组: **定义:** 枚举k元组 **做法:** 递归k层,每层[1,n] **题目:** B3621 枚举元组 **代码(注释版):** # 搜索## - 枚举元组: **定义:**枚举k元组**做法:**递归k层,每层[1,n]**题目:**B3621 枚举元组**代码(注释版):**```cpp#include<bits/stdc++.h> #include<bits/stdc++.h>using namespace std;int n, k;int a[15];void dfs(int x){ if(x == n + 1){//第x层还没选呢! if(x == n + 1){//第x层还没选呢! for(int i = 1; i <= n; i++){ cout << a[i] << " "; cout << a[i] << " "; } cout << endl; return ;//返回值没用了 多写会比不写占空间 return ;//返回值没用了 多写会比不写占空间 } for(int i = 1; i <= k; i++){ a[x] = i; dfs(x + 1);4 行未改动 dfs(1); return 0;}```## - 枚举子集: **定义:** 枚举n个元素的集合的k元子集 **做法:** 递归枚举 **题目:** B3622 枚举子集(递归实现指数型枚 举) **代码(注释版):** 二进制版(1为y 0为n ## - 枚举子集: **定义:**枚举n个元素的集合的k元子集**做法:**递归枚举**题目:**B3622 枚举子集(递归实现指数型枚 举)**代码(注释版):**二进制版(1为y 0为n```cpp#include<bits/stdc++.h>using namespace std;int n, x;void chai(int y){ string a; for(int i = 1; i <= n; i++){ if(y % 2 == 0)a += 'N'; else a += 'Y'; y /= 2; y /= 2; } int z = a.length(); for(int i = z - 1; i >= 0; i--){ cout << a[i]; } cout << endl;}int main(){ int main(){ cin >> n; x = (1 << n) - 1;//(减号优先级高)优先级问题!!!!!!!!!!!!!!! x = (1 << n) - 1;//(减号优先级高)优先级问题!!!!!!!!!!!!!!! for(int i = 0; i <= x; i++){ chai(i); chai(i); } return 0;}```超级普通的递归版本 超级普通的递归版本```cpp#include<bits/stdc++.h> #include<bits/stdc++.h>using namespace std;int n;int a[15], vis[15];void dfs(int x){ if(x == n + 1){//第x层还没选呢! if(x == n + 1){//第x层还没选呢! for(int i = 1; i <= n; i++){ if(a[i] == 1)cout <<"N"; else cout << "Y"; } cout << endl; return ;//返回值没用了 多写会比不写占空间 return ;//返回值没用了 多写会比不写占空间 } for(int i = 1; i <= 2; i++){//只有两种可能N或者Y a[x] = i; dfs(x + 1);4 行未改动 dfs(1); return 0;}```## - 枚举排列: **定义:** 就是枚举排列的所有可能 **做法:** 递归k层,每层[1,n],用标记数组保证各个元素不重复 **题目:** ## - 枚举排列:**定义:**就是枚举排列的所有可能**做法:**递归k层,每层[1,n],用标记数组保证各个元素不重复**题目:**B3623 枚举排列(递归实现排列型枚举)**代码(注释版):** **代码(注释版):**```cpp#include<bits/stdc++.h> #include<bits/stdc++.h>using namespace std;int n, k;int a[15], vis[15];void dfs(int x){ if(x == k + 1){ for(int i = 1; i <= k; i++){ cout << a[i] << " "; cout << a[i] << " "; } cout << endl; return ; }11 行未改动 dfs(1); return 0;}```## - 枚举组合: **定义:** 就是枚举组合的所有可能 **做法:** 递归k层,每层[1,n],用标记数组保证各个元素不重复,保证前一个数比后一个数小 **题目:** ## - 枚举组合:**定义:**就是枚举组合的所有可能**做法:**递归k层,每层[1,n],用标记数组保证各个元素不重复,保证前一个数比后一个数小**题目:**P10448 组合型枚举**代码(注释版):** **代码(注释版):**```cpp#include<bits/stdc++.h> #include<bits/stdc++.h>using namespace std;int n, k;int a[100], vis[150];void dfs(int x){ if(x == k + 1){ if(x == k + 1){ for(int i = 1; i <= k; i++){ cout << a[i] << " "; cout << a[i] << " "; } cout << endl; return ; }15 行未改动