Cod sursa(job #2043894)

Utilizator vazanIonescu Victor Razvan vazan Data 20 octombrie 2017 18:47:19
Problema Generare de permutari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include<cstdio>
using namespace std;
int frq[10], st[10], stpoz, n, last[10];
int main(){
    freopen("permutari.in", "r", stdin);
    freopen("permutari.out", "w", stdout);
    scanf("%d", &n);
    st[1] = 0;
    stpoz = 2;
    frq[0] = 1;
    while(stpoz){
        if(stpoz == n + 2){
            for(int i = 2; i <= n + 1; i++)
                printf("%d ", st[i]);
            printf("\n");
            stpoz--;
            continue;
        }
        if(st[stpoz] != 0){
            frq[st[stpoz]] = 0;
            st[stpoz] = 0;
        }
        last[stpoz + 1] = 0;
        while(last[stpoz]<= n){
            if(!frq[last[stpoz]]){
                st[stpoz] = last[stpoz];
                frq[last[stpoz]] = 1;
                last[stpoz]++;
                stpoz++;
                break;
            }
            last[stpoz]++;
        }
        if(last[stpoz] == n + 1)
            stpoz--;
    }
    return 0;
}