PROGRAM PERMUTATIONEN(INPUT,OUTPUT);

    VAR I,N,NFAKTORIELLE: INTEGER;
        C: ARRAY[1..10] OF INTEGER;


    FUNCTION FAK(N: INTEGER): INTEGER;
        BEGIN
            IF N=1 THEN FAK := 1
                   ELSE FAK := N*FAK(N-1)
        END (* FAK *);



    PROCEDURE WRITEPERMUTATION;
      VAR I: INTEGER;
        BEGIN
            WRITE(" ":10);
            FOR I := 1 TO N  DO WRITE( C[I] :3);
            WRITELN
        END (* WRITEPERMUTATION *);



    PROCEDURE NEXTPERMUTATION;
      VAR I,PRODUKT: INTEGER;
        BEGIN
            REPEAT
                C[N] := C[N] + N-1;
                PRODUKT := 1;
                FOR I := N DOWNTO 1 DO
                    BEGIN
                        IF C[I]>N THEN  BEGIN
                                            C[ I ] := C[ I ] - N;
                                            C[I-1] := C[I-1] + 1
                                        END;
                        PRODUKT := PRODUKT*C[I]
                    END
            UNTIL PRODUKT=NFAKTORIELLE
        END (* NEXTPERMUTATION *);



BEGIN
    WRITE("P E R M U T A T I O N E N   DER ORDNUNG");
    READ(N);
    WRITELN;
    NFAKTORIELLE := FAK(N);
    FOR I := 1 TO N  DO C[I] := I;
    WRITEPERMUTATION;
    FOR I := 2 TO NFAKTORIELLE  DO
        BEGIN
            NEXTPERMUTATION;
            WRITEPERMUTATION
        END
END.
