Created
March 27, 2020 20:58
-
-
Save thevar1able/da3416628d44087f1f03e8523ab76d6c to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <string.h> | |
#define OPEN_BRACKETS 5 | |
#define STR_LENGTH 10 | |
void gen(int length, int opened, int closed, char* str) { | |
if (opened + closed == 2 * length) { | |
printf("%s\n", str); | |
return; | |
} | |
if (opened < length) { | |
char new_str_a[STR_LENGTH]; | |
strcpy(new_str_a, str); | |
strcat(new_str_a, "("); | |
gen(length, opened + 1, closed, new_str_a); | |
} | |
if (opened > closed) { | |
char new_str_b[STR_LENGTH]; | |
strcpy(new_str_b, str); | |
strcat(new_str_b, ")"); | |
gen(length, opened, closed + 1, new_str_b); | |
} | |
} | |
int main() { | |
char n[STR_LENGTH]; | |
gen(OPEN_BRACKETS, 0, 0, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment