Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. C++:
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int combs(int rem, int open = 0) {
  6. // Base case
  7. if (rem + open == 0) {
  8. return 1;
  9. }
  10.  
  11. // Recursive part
  12. int count = 0;
  13. if (rem > 0) {
  14. count += combs(rem-1, open+1);
  15. }
  16. if (open > 0) {
  17. count += combs(rem, open-1);
  18. }
  19. return count;
  20. }
  21.  
  22. int main(int argc, char** argv) {
  23. cout << combs(13) << endl;
  24. }
  25.  
  26.  
  27.  
  28.  
  29. Haskell:
  30. combs :: Int -> Int -> Int
  31. combs rem open
  32. | rem + open == 0 = 1
  33. | rem > 0 && open > 0 = (combs (rem-1) (open+1)) + (combs rem (open-1))
  34. | rem > 0 = combs (rem-1) (open+1)
  35. | open > 0 = combs rem (open-1)
  36.  
  37. main = do
  38. putStrLn $ show $ combs 13 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement