hemel18681

permutation using recursion

Nov 29th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. bool exist(int x,vector<int> v){
  6.     for(int i=0;i<v.size();i++) if(v[i]==x) return true;
  7.     return false;
  8. }
  9.  
  10. void fun(int len, int n, vector<int> v){
  11.     if(n==0){
  12.         for(int i=0;i<v.size();i++){
  13.             cout<<v[i]<<" ";
  14.         }
  15.         cout<<endl;
  16.         return;
  17.     }
  18.     for(int i=1;i<=len;i++){
  19.         if(exist(i,v)) continue;
  20.         vector<int>tmp=v;
  21.         tmp.push_back(i);
  22.         fun(len, n-1,tmp);
  23.     }
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29.     int n;
  30.     cin>>n;
  31.     vector<int> v;
  32.     fun(n,n,v);
  33. }
Add Comment
Please, Sign In to add comment