Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<bits/stdc++.h>
- using namespace std;
- void permute(int a[],int l,int r);
- void print(int a[],int i,int n)
- {
- if(i==n)
- {
- printf("%d\n",a[i]);
- return;
- }
- else
- {
- printf("%d ",a[i]);
- print(a,i+1,n);
- }
- }
- void itr(int a[],int i,int l,int r)
- {
- if(i==r)
- {
- swap(a[i],a[l]);
- permute(a,l+1,r);
- swap(a[i],a[l]);
- return;
- }
- else
- {
- swap(a[i],a[l]);
- permute(a,l+1,r);
- swap(a[i],a[l]);
- itr(a,i+1,l,r);
- }
- }
- void permute(int a[],int l,int r)
- {
- if(l==r)
- {
- print(a,0,r);
- return;
- }
- else
- {
- int temp,i;
- itr(a,l,l,r);
- }
- }
- int main()
- {
- int n,i,a[100];
- scanf("%d",&n);
- for(i=0;i<n;i++)
- a[i]=i+1;
- permute(a,0,n-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement