回溯演算法----括號的生成
#include<string>
#include<vector>
using namespace std;
void rade(string temp, vector<string> &a,int left, int right)
{
if ((left == 0) && (right == 0))
{
a.push_back(temp);
return;
}
if (left>0)
{
rade(temp + "(", a, left - 1, right);
}
if (left<right)
{
rade(temp + ")", a, left, right - 1);
}
}
int main()
{
int n;
while (cin >> n)
{
vector<string> a;
rade("", a, n, n);
for (int i = 0; i < a.size(); i++)
{
cout << a[i] << endl;
}
}
}