您的位置:首页 > 其它

打印所有匹配括号组合

2011-09-07 14:12 274 查看
char dst[100];
void parentheses_match(char dst[], int k, int left_remain, int left_used, int right_used, int M)
{
if (right_used >= M)
{
dst[k] = '\0';
printf("%s\n",dst);
return;
}

if (left_remain <= 0 && left_used < M)
{
dst[k] = '(';
parentheses_match(dst,k+1,left_remain+1,left_used+1,right_used,M);
}
else if (left_used < M)
{
dst[k] = '(';
parentheses_match(dst,k+1,left_remain+1,left_used+1,right_used,M);
dst[k] = ')';
parentheses_match(dst,k+1,left_remain-1,left_used,right_used+1,M);
}
else
{
dst[k] = ')';
parentheses_match(dst,k+1,left_remain-1,left_used, right_used+1,M);
}
}

int _tmain(int argc, char* argv[])
{
const int M = 10;
parentheses_match(dst,0,0,0,0,M);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: