Cod sursa(job #2565308)

Utilizator ursu0406Ursu Ianis-Vlad ursu0406 Data 2 martie 2020 13:34:00
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;

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

int sol[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} , n;

bool next()
{
    int ln = 1;

    for(int i = n - 1; i >= 1; --i)
        if(sol[i] > sol[i + 1])
            ++ln;
        else
            break;

    if(ln == n) return false;

    int minswap = n + 1;

    for(int i = n; i > n - ln; --i)
        if(sol[i] > sol[n - ln] && sol[minswap] > sol[i])
            minswap = i;

    swap(sol[minswap], sol[n - ln]);

    sort(sol + n - ln + 1, sol + n + 1);

    return true;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    fin >> n;

    do {
        for(int i = 1; i <= n; ++i) fout << sol[i] << ' ';
        fout << '\n';
    }
    while(next());

}