Advertisement
Guest User

All permutations

a guest
Apr 23rd, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. static int permutations = 0;
  6.  
  7. void permute(string src, string permutation){
  8.   if(src.length() == 0){
  9.     cout << permutation << endl;
  10.     permutations++;
  11.     return;
  12.   }
  13.   for(int i = 0; i < src.length(); i++){
  14.     string newSrc = src.substr(0 ,i) + src.substr(i+1, src.length() - (i + 1));
  15.     permute(newSrc, permutation + src[i]);
  16.   }
  17. }
  18.  
  19. void permute(string src){
  20.   permute(src, "");
  21. }
  22.  
  23. int main()
  24. {
  25.     permute("1234");
  26.     cout << "Number of permutations: " << permutations << endl;
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement