估计是要按顺序输出吧顺序就是先左后右。 #include <bits/stdc++.h> using namespace std; int n; bool first = true; const int N = 1000; char a[N]; void print() { for (int i = 1; i <= 2 * n; i++) cout << a[i]; } void dfs(int l, int r) { if (l == n && r == n) { if (first) { print(); first = false; } else { cout << ","; print(); } return; } if (l > n) return; if (r > n) return; if (r > l) return; a[l+r+1] = '('; dfs(l+1, r); a[l+r+1] = ')'; dfs(l, r+1); a[l+r+1] = '\0'; } int main() { scanf("%d", &n); dfs(0, 0); cout << endl; }
点赞 评论

相关推荐

牛客网
牛客企业服务