Last active
November 5, 2019 08:47
-
-
Save pinglunliao/6e2e6e432a4615d77ee0 to your computer and use it in GitHub Desktop.
a229: 括號匹配問題 FYI: https://yunlinsong.blogspot.com/2014/11/blog-post.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
using namespace std; | |
void prtPattern(int index, int right, int left, char *pattern) { | |
if(index == right && left == index) | |
{ | |
puts(pattern - 2 * index); | |
} | |
if( right < index ) | |
{ | |
*pattern = '('; | |
prtPattern(index, right + 1, left, pattern + 1); | |
} | |
if( left < right) | |
{ | |
*pattern = ')'; | |
prtPattern(index, right, left + 1, pattern + 1); | |
} | |
} | |
int main() { | |
int n; | |
while(scanf("%d", &n) != EOF) | |
{ | |
char str[32] = {'\0'}; | |
prtPattern(n, 0, 0, str); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment