Cod sursa(job #3360685)

Utilizator skibidiciuli123luca marcu ionica serb ciuli coasta hapc hopc skibidiciuli123 Data 15 iulie 2026 18:21:32
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>
using namespace std;
#define int long long

ifstream fin("permutari.in");
ofstream fout("permutari.out");

bool nextperm(int n, vector<int> &v) {
    int i = n - 1;
    while(i >= 1 && v[i] >= v[i + 1]) {
        i--;
    }
    if(i == 0) return false;
    int j = n;
    while(v[j] <= v[i]) {
        j--;
    }
    swap(v[i], v[j]);
    int st = i + 1, dr = n;
    while(st < dr) {
        swap(v[st], v[dr]);
        st++;
        dr--;
    }
    return true;
}

signed main() 
{
    int n;
    fin >> n;
    vector <int> v(n + 1);
    for(int i = 1; i <= n; i++) {
        v[i] = i;
    }
    do {
        for(int i = 1; i <= n; i++) {
            fout << v[i] << " ";
        }
        fout << endl;
    }
    while(nextperm(n, v) == true);
}