数字 n(1<=n<=8) 代表生成括号的对数,请你算出所有可能的并且有效的括号组合的个数
例如:
输入:n = 3
满足的组合:"((()))","(()())","(())()","()(())","()()()",输出:5
输入格式:
输入在一行中给出n
输出格式:
输出个数
输入样例:
在这里给出一组输入。例如:
3
输出样例:
在这里给出相应的输出。例如:
5
代码长度限制
16 KB
时间限制
1000 ms
内存限制
#include<bits/stdc++.h>
using namespace std;
int a,ans=0;
/*只可意会不可言传*/
void dfs(int l,int r){
if((l+r)==a*2) {
ans++;
return ;
}
if(l<a) dfs(l+1,r);
if(l>r) dfs(l,r+1);
}
int main(){
cin>>a;
dfs(0,0);
cout<<ans<<endl;
return 0;
}
128 MB
栈限制
8192 KB