1、对于 scanf printf 和 cin cout 按照10^5来划分使用
递归实现指数型枚举
https://www.acwing.com/problem/content/94/
#include <iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int N=17; int n;int a[N]; void f(int u){//进行选择 0默认 1选择 2不选择 if(u>n){ for(int i=1;i<=n;i++){ if(a[i]==1) cout<<i<<" "; } cout<<endl; return;//不要忘记返回 } a[u]=1; f(u+1); a[u]=0;//递归后复原 a[u]=2; f(u+1); a[u]=0;//递归后复原 } int main() { cin>>n; f(1); return 0; }
94. 递归实现排列型枚举
https://www.acwing.com/problem/content/96/
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int N=10; int n;int st[N]; int p[N]; void dfs(int u){ if(u>n){ for(int i=1;i<=n;i++) cout<<p[i]<<" "; cout<<endl; return; } for(int i=1;i<=n;i++) {//遍历每个数字进行判断 if(!st[i]){ st[i]=1; p[u]=i;//将数字记录在该层 dfs(u+1); st[i]=0;//还原 p[u]=0; } } } int main() { cin>>n; dfs(1); return 0; }
标签:www,递归,int,dfs,蓝桥,include,递推 From: https://www.cnblogs.com/daimazhishen/p/18041534