Advertisement
193030

Recursive permutations

Nov 30th, 2020
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. // PermutationRecursion.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void calculate (string str, int left, int right)
  12. {
  13.     if(left == right)
  14.     {
  15.         cout << str << endl;
  16.     }
  17.     else
  18.     {
  19.         for (int i = left; i <= right; i++)
  20.         {
  21.             swap(str[left], str[i]);
  22.             string swapped = str;
  23.             calculate(swapped, left + 1, right);
  24.         }
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     string str = "ABC";
  31.     int sizeOfString = str.size();
  32.     calculate(str, 0, sizeOfString-1);
  33. }
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement