Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. void permute(int a[],int l,int r);
  5. void print(int a[],int i,int n)
  6. {
  7.     if(i==n)
  8.     {
  9.         printf("%d\n",a[i]);
  10.         return;
  11.     }
  12.  
  13.     else
  14.     {
  15.         printf("%d ",a[i]);
  16.         print(a,i+1,n);
  17.     }
  18. }
  19. void itr(int a[],int i,int l,int r)
  20. {
  21.     if(i==r)
  22.     {
  23.         swap(a[i],a[l]);
  24.         permute(a,l+1,r);
  25.         swap(a[i],a[l]);
  26.         return;
  27.     }
  28.     else
  29.     {
  30.         swap(a[i],a[l]);
  31.             permute(a,l+1,r);
  32.             swap(a[i],a[l]);
  33.         itr(a,i+1,l,r);
  34.     }
  35. }
  36. void permute(int a[],int l,int r)
  37. {
  38.     if(l==r)
  39.     {
  40.         print(a,0,r);
  41.         return;
  42.     }
  43.  
  44.     else
  45.     {
  46.         int temp,i;
  47.         itr(a,l,l,r);
  48.  
  49.     }
  50. }
  51. int main()
  52. {
  53.     int n,i,a[100];
  54.     scanf("%d",&n);
  55.     for(i=0;i<n;i++)
  56.         a[i]=i+1;
  57.     permute(a,0,n-1);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement