/*
四对括号可以有多少种匹配排列方式?比如两对括号可以有两种 : ( ) ()和( ( ))
*/
#include<iostream>
using namespace std;
void dfs( int left , int right , int length , char str[] ) {
if( right == length ) {
//如果右括号数量等于n,那就意味着一种匹配模式找到了
cout << str << endl;
}
else {
//如果可以放左括号就放左括号
if( left < length ) {
str[ left + right ] = '(';
str[ left + right + 1 ] = '\0';
dfs( left + 1 , right , length , str );
}
//当左括号数量大于右括号的时候可以放右括号
if( left > right ) {
str[ left + right ] = ')';
str[ left + right + 1 ] = '\0';
dfs( left , right + 1 , length , str );
}
}
}
int main( ) {
char str[ 100 ];
int N;
cin >> N;
dfs( 0 , 0 , N , str );
}
括弧匹配方式有几种?
最新推荐文章于 2025-02-06 00:15:00 发布