2

使用 java,查找安排非常容易且可计算。

使用 COBOL 作为编程语言,我发现很难对其进行编码。

PERFORM VARYING I FROM 1 BY 1 UNTIL I=5
 COMPUTE X = 5-I                        
 PERFORM VARYING J FROM I BY 1 UNTIL J=X
    MOVE WORD(I,1) TO TEMP1.            
    MOVE WORD(J,1) TO TEMP2.            

我有一个代码示例,我不确定它是否不完整,我想知道我的做法是否正确。

4

4 回答 4

3

这个问题可以使用几乎任何编程语言(包括 COBOL)来解决,无论是否使用递归。递归解决方案是微不足道的,非递归解决方案更有趣。这是一个 COBOL,非递归实现。

几点观察:

可以从空的基本情况和用于构建排列的字符列表迭代地构建排列。在每次迭代中,下一个字符从输入列表中取出,并插入到前一次迭代中基本情况的所有可能位置。这些新字符串成为下一次迭代的基本情况。当输入列表为空时,基本情况包含所有可能的排列。

考虑以这种方式构建 3 个字符的排列:

Iteration 0 => Input[a, b, c] introduce []: BaseCases[[]]
Iteration 1 => Input[b, c] introduce [a]: BaseCases[[a]]
Iteration 2 => Input[c] introduce [b]: BaseCases[[b a], [a b]]
Iteration 3 => Input [] introduce [c]: BaseCases[[c b a], [b c a], [b a c], [c b a], [b c a], [a b c]]

请注意,最终基本情况中的字符串数等于阶乘(长度(输入))。在上面的示例中,引入了三个字符,给出 3! = 最终基本情况中的 6 个字符串。

由于在任何给定迭代中要生成的字符串的数量在迭代时是已知的,并且该数量大于前一次迭代,我们可以使用相同的数组结构同时保存两个迭代的基本情况。这是通过从最高下标开始构建新的基本案例并向后工作来完成的。提取在先前迭代中生成的基本模式时也是如此。这解释了为什么以下程序中的下标以相反的顺序完成(倒计时而不是倒计时)。

   IDENTIFICATION DIVISION.
   PROGRAM-ID ARRANGE.

   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01.
       02 ARRANGEMENT-TABLE.
          03 ARRANGEMENT PIC X(5) OCCURS 120 TIMES.
   01  INPUT-CHARS       PIC X(5) VALUE 'abcde'.
   01  BASE-ARRANGEMENT  PIC X(5).
   01  CURR-CHAR         PIC X.
   01  I                 PIC S9(4) BINARY.
   01  J                 PIC S9(4) BINARY.
   01  K                 PIC S9(4) BINARY.
   01  L                 PIC S9(4) BINARY.
   01  CURR-FACT         PIC S9(9) BINARY.
   01  PREV-FACT         PIC S9(9) BINARY.
   01  COMP-FACT         PIC S9(9) BINARY.
   PROCEDURE DIVISION.
  *
  *    Verify that the Arrangement table is large enough to hold
  *    all possible arrangements of the letters in INPUT-CHARS.
  *
       COMPUTE COMP-FACT =
               FUNCTION FACTORIAL (LENGTH OF INPUT-CHARS)
       IF COMP-FACT > LENGTH OF ARRANGEMENT-TABLE /
                      LENGTH OF ARRANGEMENT(1)
          DISPLAY 'ARRANGEMENT-TABLE is too small.'
          GOBACK
       END-IF
       IF LENGTH OF ARRANGEMENT(1) < LENGTH OF INPUT-CHARS
          DISPLAY 'INPUT-CHARS too long for ARRANGEMENT.'
          GOBACK
       END-IF
       IF LENGTH OF BASE-ARRANGEMENT < LENGTH OF ARRANGEMENT(1)
          DISPLAY 'BASE-ARRANGEMENT is too small for ARRANGEMENT.'
          GOBACK
       END-IF

       MOVE SPACES TO ARRANGEMENT(1)

       DISPLAY 'Starting sequence: ' INPUT-CHARS
  *
  *    Generate all possible arrangements of INPUT-CHARS...
  *
  *       I counts through the set of INPUT-CHARS used in string geneation
  *       J counts down from arrangements built on previous iteration
  *       K counts to number of characters in new expanded base case
  *       L counts down from arrangements to be build in current iteration
  *
  *       CURR-FACT is the factorial of the current iteration number
  *       PREV-FACT is the factorial of the previous iteration
  *       CURR-CHAR is the character to add into existing base cases

       MOVE 1 TO CURR-FACT
       PERFORM VARYING I FROM 1 BY 1
                 UNTIL I > LENGTH OF INPUT-CHARS
          MOVE CURR-FACT TO PREV-FACT
          COMPUTE CURR-FACT = PREV-FACT * I
          MOVE INPUT-CHARS(I:1) TO CURR-CHAR
          MOVE CURR-FACT TO L
          PERFORM VARYING J FROM PREV-FACT BY -1
                    UNTIL J = ZERO
             PERFORM VARYING K FROM 1 BY 1
               UNTIL K > I
               MOVE ARRANGEMENT(J) TO BASE-ARRANGEMENT
               PERFORM NEW-ARRANGEMENT
               COMPUTE L = L - 1
             END-PERFORM
          END-PERFORM
       END-PERFORM
  *
  *    List generated patters...
  *
       COMPUTE COMP-FACT =
               FUNCTION FACTORIAL(LENGTH OF INPUT-CHARS)
       PERFORM VARYING I FROM COMP-FACT BY -1
                 UNTIL I < 1
          DISPLAY ARRANGEMENT(I)
       END-PERFORM
       GOBACK
       .
   NEW-ARRANGEMENT.
  *
  *    Build a new character arrangement by placing
  *    CURR-CHAR into position K of a given
  *    BASE-ARRANGEMENT
  *
       MOVE SPACES    TO ARRANGEMENT(L)
       MOVE CURR-CHAR TO ARRANGEMENT(L)(K:1)
       IF K > 1
          MOVE BASE-ARRANGEMENT(1:K - 1)
            TO ARRANGEMENT(L)(1:K - 1)
       END-IF
       IF K <= PREV-FACT
          MOVE BASE-ARRANGEMENT(K:)
            TO ARRANGEMENT(L)(K + 1:)
       END-IF
       .

最后说明: 如果输入字符串包含重复字符,则某些模式将重复。该解决方案没有考虑到这种“复杂性”。

于 2013-08-07T15:00:46.303 回答
3

这是 Ingo 蓝图(见上文)在 COBOL 中的实现。我提供它是因为它与 NealB 和 Bill Woodger 提供的算法不同。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. PERMUTATION.
  * GENERATE PERMUTATIONS FROM A SET OF FIVE ELEMENTS
  * WITHOUT USING RECURSION

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

   DATA DIVISION.
   FILE SECTION.

   WORKING-STORAGE SECTION.
   01  CHAR-SET                    PIC X(5) VALUE 'ABCDE'.
   01  SOLUTION                    PIC X(5).
   01  SOLUTION-COUNT              PIC 999 VALUE ZERO.
   01  INDEXES.
       02  I                       PIC 9.
       02  J                       PIC 9.
       02  K                       PIC 9.
       02  L                       PIC 9.
       02  M                       PIC 9.

   PROCEDURE DIVISION.
   MAIN.
  * IGNORED REGULAR INDENTING TO SIMPLIFY CODE
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
       PERFORM VARYING J FROM 1 BY 1 UNTIL J > 5
       IF J NOT = I
       PERFORM VARYING K FROM 1 BY 1 UNTIL K > 5
       IF K NOT = J AND K NOT = I
       PERFORM VARYING L FROM 1 BY 1 UNTIL L > 5
       IF L NOT = K AND L NOT = J AND L NOT = I
       PERFORM VARYING M FROM 1 BY 1 UNTIL M > 5
       IF M NOT = L AND M NOT = K AND M NOT = J AND M NOT = I
            ADD 1 TO SOLUTION-COUNT END-ADD
            DISPLAY SOLUTION-COUNT ' ' WITH NO ADVANCING
            END-DISPLAY
            MOVE CHAR-SET(I:1) TO SOLUTION (1:1)
            MOVE CHAR-SET(J:1) TO SOLUTION (2:1)
            MOVE CHAR-SET(K:1) TO SOLUTION (3:1)
            MOVE CHAR-SET(L:1) TO SOLUTION (4:1)
            MOVE CHAR-SET(M:1) TO SOLUTION (5:1)
            DISPLAY SOLUTION END-DISPLAY
       END-IF
       END-PERFORM
       END-IF
       END-PERFORM
       END-IF
       END-PERFORM
       END-IF
       END-PERFORM
       END-PERFORM
       STOP RUN
       .

于 2013-08-09T01:14:15.857 回答
1

恕我直言,您需要一个 n 折嵌套循环来排列 n 个元素。以下提供了一个蓝图:

for i = 1 to 5
  for j = 1 to 5 if j != i
    for k = 1 to 5 if k != j && k != i
      for m = 1 to 5 if m != k && m != j && m != i
        for n = 1 to 5 if n != m && n != k && n != j && n != i
          solution = (i,j,k,m,n)

这样,您将获得 120 个解决方案。如果需要,您可以将索引替换为相应位置的实际字符。

要使用 SQL 解决它,假设我们有一个包含 5 个不同行的表:

CREATE NUMBERS (VAL INT PRIMARY KEY);
INSERT INTO NUMBERS VALUES(1);
INSERT INTO NUMBERS VALUES(2);
INSERT INTO NUMBERS VALUES(3);
INSERT INTO NUMBERS VALUES(4);
INSERT INTO NUMBERS VALUES(5);
SELECT VAL I, VAL J, VAL K, VAL M, VAL N FROM NUMBERS WHERE
    I <> J 
    AND K <> I AND K <> J
    AND M <> I AND M <> J AND M <> K
    AND N <> I AND N <> J AND N <> K AND N <> M;

不确定 SQL 语法是否正确,但您明白了。

于 2013-08-06T11:52:14.487 回答
1

将“值 1”固定在位置一,通过循环一次生成 24 个组合。

然后,“首先为它腾出空间”,将值 1 放入第 2 列。然后将值 1 放入第 3 列。第 4 列。第 5 列。

或者

取初始结果,即一组,然后“旋转”结果中的所有值(1 = 2、2 = 3、3 = 4、4 = 5、5 = 1)。那是第二个结果集。再做三遍。

使用任何一种方法,您都可以包含一次迭代的“完整结果”以获得 24 个结果的“模式”。

例如,可以将以下内容添加到 Valdis Grinbergs 的代码中:

在工作存储部分:

01  SAVE-SOLUTION.
    02  SAVE-IT                 PIC X.
    02  FILLER                  PIC X(4).

在程序部分:

将第一个 PERFORM 更改为

MOVE 1 TO I

显示每个解决方案后:

PERFORM SOLUTIONS-FOR-REMAINING-VALUES

也就是说,对于“列插入”版本:

   SOLUTIONS-FOR-REMAINING-VALUES.
       MOVE SOLUTION                TO SAVE-SOLUTION
       MOVE SOLUTION (2:1)          TO SOLUTION (1:1)
       MOVE SAVE-IT                 TO SOLUTION (2:1) 
       ADD 1                        TO SOLUTION-COUNT 
       DISPLAY SOLUTION 
       MOVE SAVE-SOLUTION           TO SOLUTION
       MOVE SOLUTION (3:1)          TO SOLUTION (1:1)
       MOVE SAVE-IT                 TO SOLUTION (3:1) 
       ADD 1                        TO SOLUTION-COUNT 
       DISPLAY SOLUTION 
       MOVE SAVE-SOLUTION           TO SOLUTION
       MOVE SOLUTION (4:1)          TO SOLUTION (1:1)
       MOVE SAVE-IT                 TO SOLUTION (4:1) 
       ADD 1                        TO SOLUTION-COUNT 
       DISPLAY SOLUTION 
       MOVE SAVE-SOLUTION           TO SOLUTION
       MOVE SOLUTION (5:1)          TO SOLUTION (1:1)
       MOVE SAVE-IT                 TO SOLUTION (5:1) 
       ADD 1                        TO SOLUTION-COUNT 
       DISPLAY SOLUTION 
       .

对于“重复换位”版本:

   SOLUTIONS-FOR-REMAINING-VALUES.
       PERFORM 4 TIMES
           MOVE SOLUTION (1:1) TO SAVE-IT
           MOVE SOLUTION (2:1) TO SOLUTION (1:1)
           MOVE SOLUTION (3:1) TO SOLUTION (2:1)
           MOVE SOLUTION (4:1) TO SOLUTION (3:1)
           MOVE SOLUTION (5:1) TO SOLUTION (4:1)
           MOVE SAVE-IT TO SOLUTION (5:1)
           ADD 1 TO SOLUTION-COUNT 
           DISPLAY SOLUTION 
       END-PERFORM
       .

当然,我添加的段落可以作为循环来完成,但我想集中展示正在发生的事情,而不是如何在 COBOL 中编写循环。

这两种不同的“解决方案”实际上是同一事物的两种实现。一旦建立了“模式”,就可以通过简单地改变固定模式内的内容来生成另外 4/5 的输出。

可以处理原始代码中的循环。

如果对于实际应用程序,性能是主要要求,那么只需在编写一行代码之前意识到“模式”是已知的。该代码只是节省了您手动编写 24 个结果的时间。为了性能,并且使用足够小的“模式”,只需将其编码,忘记循环。

而且我自己不会使用所有的“参考修改”,我只是把它从原来的地方留在那里。

现在,两个版本没有循环。基于前 24 个解决方案的“模式”是事先已知的,并且剩余的解决方案(已经知道,但不需要以相同的方式编码)可以很容易地从那。

旋转值。

ID DIVISION.
PROGRAM-ID. PROTATE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  CHAR-SET                    PIC X(5) VALUE 'ABCDE'.
01  SOLUTION                    PIC X(5).
01  SOLUTION-COUNT     binary   PIC 9(4) VALUE ZERO.
01  INDEXES.
    02  COLUMN-1              binary         PIC 9(4).
    02  COLUMN-2              binary         PIC 9(4).
    02  COLUMN-3              binary         PIC 9(4).
    02  COLUMN-4              binary         PIC 9(4).
    02  COLUMN-5              binary         PIC 9(4).
    02  ICOL-3                binary         PIC 9(4).
    02  ICOL-4                binary         PIC 9(4).
    02  ICOL-5                binary         PIC 9(4).
01  SAVE-SOLUTION.
    02  SAVE-IT                 PIC X.
    02  FILLER                  PIC X(4).

PROCEDURE DIVISION.
MAIN-para.
    MOVE 1 TO COLUMN-1 
    MOVE 2 TO COLUMN-2
    MOVE 3 TO ICOL-3
    MOVE 4 TO ICOL-4
    MOVE 5 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    MOVE 3 TO COLUMN-2
    MOVE 2 TO ICOL-3
    MOVE 4 TO ICOL-4
    MOVE 5 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    MOVE 4 TO COLUMN-2
    MOVE 2 TO ICOL-3
    MOVE 3 TO ICOL-4
    MOVE 5 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    MOVE 5 TO COLUMN-2
    MOVE 2 TO ICOL-3
    MOVE 3 TO ICOL-4
    MOVE 4 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    DISPLAY SOLUTION-COUNT
    STOP RUN
    .
SIX-SOLUTIONS.
    MOVE ICOL-3 TO COLUMN-3
    MOVE ICOL-4 TO COLUMN-4
    MOVE ICOL-5 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-5 TO COLUMN-4
    MOVE ICOL-4 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-4 TO COLUMN-3
    MOVE ICOL-3 TO COLUMN-4
    MOVE ICOL-5 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-5 TO COLUMN-4
    MOVE ICOL-3 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-5 TO COLUMN-3
    MOVE ICOL-3 TO COLUMN-4
    MOVE ICOL-4 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-4 TO COLUMN-4
    MOVE ICOL-3 TO COLUMN-5
    PERFORM A-SOLUTION
    .
A-SOLUTION.
    MOVE CHAR-SET ( 1 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE CHAR-SET ( COLUMN-2 : 1 ) TO SOLUTION ( 2 : 1 )
    MOVE CHAR-SET ( COLUMN-3 : 1 ) TO SOLUTION ( 3 : 1 )
    MOVE CHAR-SET ( COLUMN-4 : 1 ) TO SOLUTION ( 4 : 1 )
    MOVE CHAR-SET ( COLUMN-5 : 1 ) TO SOLUTION ( 5 : 1 )
    PERFORM SOLUTION-READY
    PERFORM SOLUTIONS-FOR-REMAINING-VALUES
    .
SOLUTIONS-FOR-REMAINING-VALUES.
    MOVE SOLUTION TO SAVE-SOLUTION
    MOVE SOLUTION ( 2 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 2 : 1 ) 
    PERFORM SOLUTION-READY
    MOVE SAVE-SOLUTION TO SOLUTION
    MOVE SOLUTION ( 3 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 3 : 1 ) 
    PERFORM SOLUTION-READY
    MOVE SAVE-SOLUTION TO SOLUTION
    MOVE SOLUTION ( 4 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 4 : 1 ) 
    PERFORM SOLUTION-READY
    MOVE SAVE-SOLUTION TO SOLUTION
    MOVE SOLUTION ( 5 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 5 : 1 ) 
    PERFORM SOLUTION-READY
    .
SOLUTION-READY.
    ADD 1 TO SOLUTION-COUNT 
    DISPLAY SOLUTION 
    .

列插入:

ID DIVISION.
PROGRAM-ID. PCOLINS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  CHAR-SET                    PIC X(5) VALUE 'ABCDE'.
01  SOLUTION                    PIC X(5).
01  SOLUTION-COUNT     binary   PIC 9(4) VALUE ZERO.
01  INDEXES.
    02  COLUMN-1              binary         PIC 9(4).
    02  COLUMN-2              binary         PIC 9(4).
    02  COLUMN-3              binary         PIC 9(4).
    02  COLUMN-4              binary         PIC 9(4).
    02  COLUMN-5              binary         PIC 9(4).
    02  ICOL-3                binary         PIC 9(4).
    02  ICOL-4                binary         PIC 9(4).
    02  ICOL-5                binary         PIC 9(4).
01  SAVE-SOLUTION.
    02  SAVE-IT                 PIC X.
    02  FILLER                  PIC X(4).     

PROCEDURE DIVISION.
MAIN-para.
    MOVE 1 TO COLUMN-1 
    MOVE 2 TO COLUMN-2
    MOVE 3 TO ICOL-3
    MOVE 4 TO ICOL-4
    MOVE 5 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    MOVE 3 TO COLUMN-2
    MOVE 2 TO ICOL-3
    MOVE 4 TO ICOL-4
    MOVE 5 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    MOVE 4 TO COLUMN-2
    MOVE 2 TO ICOL-3
    MOVE 3 TO ICOL-4
    MOVE 5 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    MOVE 5 TO COLUMN-2
    MOVE 2 TO ICOL-3
    MOVE 3 TO ICOL-4
    MOVE 4 TO ICOL-5
    PERFORM SIX-SOLUTIONS
    DISPLAY SOLUTION-COUNT
    STOP RUN
    .
SIX-SOLUTIONS.
    MOVE ICOL-3 TO COLUMN-3
    MOVE ICOL-4 TO COLUMN-4
    MOVE ICOL-5 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-5 TO COLUMN-4
    MOVE ICOL-4 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-4 TO COLUMN-3
    MOVE ICOL-3 TO COLUMN-4
    MOVE ICOL-5 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-5 TO COLUMN-4
    MOVE ICOL-3 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-5 TO COLUMN-3
    MOVE ICOL-3 TO COLUMN-4
    MOVE ICOL-4 TO COLUMN-5
    PERFORM A-SOLUTION
    MOVE ICOL-4 TO COLUMN-4
    MOVE ICOL-3 TO COLUMN-5
    PERFORM A-SOLUTION
    .
A-SOLUTION.
    MOVE CHAR-SET ( 1 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE CHAR-SET ( COLUMN-2 : 1 ) TO SOLUTION ( 2 : 1 )
    MOVE CHAR-SET ( COLUMN-3 : 1 ) TO SOLUTION ( 3 : 1 )
    MOVE CHAR-SET ( COLUMN-4 : 1 ) TO SOLUTION ( 4 : 1 )
    MOVE CHAR-SET ( COLUMN-5 : 1 ) TO SOLUTION ( 5 : 1 )
    PERFORM SOLUTION-READY
    PERFORM SOLUTIONS-FOR-REMAINING-VALUES
    .
SOLUTIONS-FOR-REMAINING-VALUES.
    MOVE SOLUTION TO SAVE-SOLUTION
    MOVE SOLUTION ( 2 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 2 : 1 ) 
    PERFORM SOLUTION-READY
    MOVE SAVE-SOLUTION TO SOLUTION
    MOVE SOLUTION ( 3 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 3 : 1 ) 
    PERFORM SOLUTION-READY
    MOVE SAVE-SOLUTION TO SOLUTION
    MOVE SOLUTION ( 4 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 4 : 1 ) 
    PERFORM SOLUTION-READY
    MOVE SAVE-SOLUTION TO SOLUTION
    MOVE SOLUTION ( 5 : 1 ) TO SOLUTION ( 1 : 1 )
    MOVE SAVE-IT TO SOLUTION ( 5 : 1 ) 
    PERFORM SOLUTION-READY
    .
SOLUTION-READY.
    ADD 1 TO SOLUTION-COUNT 
    DISPLAY SOLUTION 
    .

好的。问题的具体程序。有点。预期的答案可能是 Ingo/Valdis Grinbergs 的答案,用于学习如何“嵌套”循环。

程序在做什么?好吧,它得到了 1/5 的排列,然后依靠这些结果中的对称性来生成剩余的 4/5 排列,除了重新排列之外没有进一步的处理。

为什么没有循环?因为,既然是预先知道的,那么答案是预先知道的。与总是产生相同结果的循环不同,结果是“硬编码”的。

程序可以通用化吗?是的。算法是什么?

好吧,您可以描述代码,并弄清楚如何扩展它。或者你可以看看数据。

六对二的产生是做什么的?好吧,两个对只是两个值的排列。六、三个值的排列。做六四次是四个值的排列。

因此,要对五个值进行烫发,请将五个单独的值中的每一个应用于四个值的排列模式。要允许四个值,请将四个单独的值中的每一个应用于三个值的排列模式。要允许三个值,请将三个单独的值中的每一个应用于两个值的排列模式。要 perm 两个值,请将两个单独的值中的每一个应用于一个值的排列模式(!)。

因此,要 perm N 个值,请将 N 个单独的值中的每一个应用于 (N-1) 个值的排列模式。

在一般解决方案中,N = 1 需要零次迭代。N = 2 需要一次迭代。N = 3 需要两次迭代。N = 4 需要六次迭代。N = 5 需要 24 次迭代。N = N 需要 (N - 1)!迭代,其中 N = 1 是一种特殊情况。

要生成所有数据,而不是硬编码初始解决方案,需要求和。N = 5,从没有可用的较小排列的起点开始,需要 24 + 6 + 2 + 1 = 33 次迭代。

是的,这很容易用于递归解决方案。它还适用于完全没有循环的解决方案。这不是 COBOL 特定的,但对于任何语言都是一样的。

当然,对于每个不同的 N 值,您永远不需要每个程序多次调用。再说一次,使用递归没问题。

COBOL 中的递归问题是 COBOL 程序员普遍不熟悉如何做到这一点。

“光滑”版本的明显用途是如果必须处理“大”尺寸的 N(涉及阶乘,因此“大”会很快到达)。

另一件事是“清晰”。下一个人能否理解代码在做什么。

如果我能找到时间,我会做一个“不错”的版本..,

于 2013-08-07T08:06:59.987 回答