Advertisement
Guest User

Kupa

a guest
Jun 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int P[] = {1,2,3,4,5,6,7,8,9};
  6.  
  7. void swap(int x, int y){
  8.     int temp = P[x];
  9.     P[x]=P[y];
  10.     P[y]=temp;
  11. }
  12.  
  13. void print(int n){
  14.     int i;
  15.     for (i=0;i<n;i++)
  16.         cout << P[i] << " ";
  17.  
  18.     cout << endl;
  19. }
  20.  
  21. void permute(int k, int n){
  22.     int i;
  23.  
  24.     if (k==0)
  25.         print(n);
  26.     else{
  27.         for (i=k-1;i>=0;i--){
  28.             swap(i,k-1);
  29.             permute(k-1, n);
  30.             swap(i,k-1);
  31.         }
  32.     }
  33. }
  34.  
  35. int main(){
  36.  
  37.     permute(3, 3);
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement