1.终止程序条件(if)
2.迭代每一层内容(for , if )
#include <QCoreApplication>
#include <QDebug>
#include <QVector>
#include <QString>
QVector<QString> vec ;
void back(QString str ,int n , int left_t ,int right_t)
{
if (left_t == n && right_t == n )
{
vec.push_back(str) ;
return ;
}
if (left_t < right_t)
{
return ;
}
if (left_t < n )
{
str += '(' ;
back(str,n,left_t+1,right_t) ;
str = str.left(str.size()-1) ;
}
if (right_t < n)
{
str += ')' ;
back(str,n,left_t,right_t +1);
str = str.left(str.size()-1) ;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int t=3 ;
QString str = "" ;
back(str,3,0,0);
for (auto c : vec)
{
qDebug() << c ;
}
return a.exec();
}
文章评论