I. Question
How many matching arrangement methods can be applied to the four pairs of parentheses? For example, two pairs of parentheses can have two types: () and (())
Ii. Answers
Solution 1: one pair of parentheses is offset.
We can regard the left parenthesis as the 1 right parenthesis.
-1, and then arrange the 8 numbers in full
Determine whether or not each sorting meets the conditions and add them one by one. Sum> = 0 until the traversal of the sequence is complete. If the conditions are met, Count ++
Failed if sum <0 appears
Solution 2: Use eight bits, from 0000
Traversal from 0000 to 1111 1111, with zero Addition
-1: 1 plus
1
If all the bits in the sequence are equal to 0 and sum is always greater than zero during incremental addition, the conditions are met.
3. Source Code
#include<iostream>#include <vector>using namespace std ;void Print(vector<char> v){for (vector<char>::iterator beg=v.begin();beg!=v.end();++beg)cout<<*beg<<" ";cout<<endl;}void MatchNums(int nSize,int nLen,vector<char> &v){int nLeftBrackets=0;int nRightBrackets=0;for (vector<char>::iterator beg=v.begin();beg!=v.end();++beg){if(*beg=='(')nLeftBrackets++;elsenRightBrackets++;if(nRightBrackets>nLeftBrackets)return;if(nLeftBrackets+nRightBrackets==nSize&&nLeftBrackets==nRightBrackets)Print(v);}if (nLen>0){v.push_back('(');MatchNums(nSize,nLen-1,v);v.pop_back();v.push_back(')');MatchNums(nSize,nLen-1,v);v.pop_back();}}int main(){vector <char> v;int n=4;MatchNums(n,n,v);return 1;}