Cod sursa(job #2969819)

Utilizator sandry24Grosu Alexandru sandry24 Data 23 ianuarie 2023 19:02:16
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

vector<vi> ans;

void all_perm(int l, int r, vi &a){
    if(l == r){
        ans.pb(a);
    } else {
        for(int i = l; i < r; i++){
            swap(a[l], a[i]);
            all_perm(l+1, r, a);
            swap(a[l], a[i]);
        }
    }
}
 
void solve(){
    int n;
    cin >> n;
    vi a;
    for(int i = 1; i <= n; i++)
        a.pb(i);
    all_perm(0, n, a);
    sort(ans.begin(), ans.end());
    for(auto i : ans){
        for(auto j : i)
            cout << j << ' ';
        cout << '\n';
    }
}  
 
int main(){
    freopen("permutari.in", "r", stdin);
    freopen("permutari.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}