Created
July 25, 2016 02:50
-
-
Save jniemann66/294a09d5ab853e4844ff9694ec00813d 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
// Parenthesis.cpp : calculate all expressions containing n pairs of matched parentheses ... | |
// | |
#include <string> | |
#include <iostream> | |
void brackets(int availableOpenBrackets, int availableCloseBrackets, std::string s, int& combinationCount); | |
int main() | |
{ | |
int combinations = 0; // Note: look-up "Catalan numbers" (1,1,2,5,14,42,132,429, ... ) | |
brackets(5, 0, " ",combinations); | |
std::cout << "Combinations: " << combinations << std::endl; | |
return 0; | |
} | |
void brackets(int availableOpenBrackets, int availableCloseBrackets, std::string s, int& combinationCount){ | |
if ((availableOpenBrackets == 0) && (availableCloseBrackets == 0)) { | |
++combinationCount; | |
std::cout << s << std::endl; | |
} | |
if (availableOpenBrackets > 0) { | |
brackets(availableOpenBrackets - 1, availableCloseBrackets + 1, s + "<", combinationCount); | |
} | |
if (availableCloseBrackets > 0) { | |
brackets(availableOpenBrackets, availableCloseBrackets - 1, s + ">", combinationCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment