| 1 | PROGRAM PERMUTATIONEN(INPUT,OUTPUT); |
| 2 | |
| 3 | VAR I,N,NFAKTORIELLE: INTEGER; |
| 4 | C: ARRAY[1..10] OF INTEGER; |
| 5 | |
| 6 | |
| 7 | FUNCTION FAK(N: INTEGER): INTEGER; |
| 8 | BEGIN |
| 9 | IF N=1 THEN FAK := 1 |
| 10 | ELSE FAK := N*FAK(N-1) |
| 11 | END (* FAK *); |
| 12 | |
| 13 | |
| 14 | |
| 15 | PROCEDURE WRITEPERMUTATION; |
| 16 | VAR I: INTEGER; |
| 17 | BEGIN |
| 18 | WRITE(" ":10); |
| 19 | FOR I := 1 TO N DO WRITE( C[I] :3); |
| 20 | WRITELN |
| 21 | END (* WRITEPERMUTATION *); |
| 22 | |
| 23 | |
| 24 | |
| 25 | PROCEDURE NEXTPERMUTATION; |
| 26 | VAR I,PRODUKT: INTEGER; |
| 27 | BEGIN |
| 28 | REPEAT |
| 29 | C[N] := C[N] + N-1; |
| 30 | PRODUKT := 1; |
| 31 | FOR I := N DOWNTO 1 DO |
| 32 | BEGIN |
| 33 | IF C[I]>N THEN BEGIN |
| 34 | C[ I ] := C[ I ] - N; |
| 35 | C[I-1] := C[I-1] + 1 |
| 36 | END; |
| 37 | PRODUKT := PRODUKT*C[I] |
| 38 | END |
| 39 | UNTIL PRODUKT=NFAKTORIELLE |
| 40 | END (* NEXTPERMUTATION *); |
| 41 | |
| 42 | |
| 43 | |
| 44 | BEGIN |
| 45 | WRITE("P E R M U T A T I O N E N DER ORDNUNG"); |
| 46 | READ(N); |
| 47 | WRITELN; |
| 48 | NFAKTORIELLE := FAK(N); |
| 49 | FOR I := 1 TO N DO C[I] := I; |
| 50 | WRITEPERMUTATION; |
| 51 | FOR I := 2 TO NFAKTORIELLE DO |
| 52 | BEGIN |
| 53 | NEXTPERMUTATION; |
| 54 | WRITEPERMUTATION |
| 55 | END |
| 56 | END. |