順列

 投稿者:しばっち  投稿日:2013年11月 3日(日)00時12分36秒
  PUBLIC NUMERIC B(10),S
DO
   INPUT PROMPT "N,R=":N,R
LOOP UNTIL N>=R
PRINT PERM(N,R)
SELECT CASE R
CASE 2
   CALL PERM2(N)
CASE 3
   CALL PERM3(N)
CASE 4
   CALL PERM4(N)
CASE 5
   CALL PERM5(N)
CASE ELSE
   DIM A(N)
   CALL PERMN(1,N,R,A)
END SELECT
END

EXTERNAL SUB PERM2(N)
FOR I=1 TO N
   FOR J=1 TO N
      IF I<>J THEN
         LET S=S+1
         PRINT S;":";I;J
      END IF
   NEXT J
NEXT I
END SUB

EXTERNAL SUB PERM3(N)
FOR I=1 TO N
   FOR J=1 TO N
      FOR K=1 TO N
         IF I<>J AND J<>K AND I<>K THEN
            LET S=S+1
            PRINT S;":";I;J;K
         END IF
      NEXT K
   NEXT J
NEXT I
END SUB

EXTERNAL SUB PERM4(N)
FOR I=1 TO N
   FOR J=1 TO N
      FOR K=1 TO N
         FOR L=1 TO N
            IF I<>J AND I<>K AND I<>L AND J<>K AND J<>L AND K<>L THEN
               LET S=S+1
               PRINT S;":";I;J;K;L
            END IF
         NEXT L
      NEXT K
   NEXT J
NEXT I
END SUB

EXTERNAL SUB PERM5(N)
FOR I=1 TO N
   FOR J=1 TO N
      FOR K=1 TO N
         FOR L=1 TO N
            FOR M=1 TO N
               IF I<>J AND I<>K AND I<>L AND I<>M AND J<>K AND J<>L AND J<>M AND K<>L AND K<>M AND L<>M THEN
                  LET S=S+1
                  PRINT S;":";I;J;K;L;M
               END IF
            NEXT M
         NEXT L
      NEXT K
   NEXT J
NEXT I
END SUB

EXTERNAL  SUB PERMN(NN,M,N,A())
IF N<NN THEN
   MAT B=ZER
   FOR J=1 TO N
      IF B(A(J))<>0 THEN EXIT SUB
      LET B(A(J))=1
   NEXT J
   LET S=S+1
   PRINT S;":";
   FOR J=1 TO N
      PRINT A(J);
   NEXT J
   PRINT
ELSE
   FOR I=1 TO M
      LET A(NN)=I
      CALL PERMN(NN+1,M,N,A)
   NEXT I
END IF
END SUB
 

戻る