Advertisement
heranchris0430

排列組合-Sucessful

May 10th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5. void perm(int *ch,int a,int b);
  6.  
  7.     int n;
  8.     int c=0;
  9.  
  10. int main()
  11. {
  12.     cout << "enter a number you want to perm : "<<endl;
  13.     cin >> n;
  14.     int list[n];
  15.     for(int i=0;i<n;i++)
  16.         list[i] = i+1;
  17.    
  18.     perm(list,0,n-1);
  19.     cout << c <<" type for " << n << " permutation"<<endl;
  20.     system("pause");
  21.     return 0;
  22. }
  23.  
  24.  
  25. void perm(int *ch,int a,int b)
  26. {
  27.      if(a==b){
  28.         for(int i=0;i<n;i++)
  29.             cout << ch[i];
  30.         cout << endl;
  31.         c++;
  32.      }
  33.      else
  34.         for(int j=a;j<n;j++){        
  35.             swap(ch[a],ch[j]);
  36.             perm(ch,a+1,b);
  37.             swap(ch[a],ch[j]);
  38.         }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement