使用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.
我有一个代码示例,我不确定它是不完整的,我想知道我是否采取了正确的方式。
答案 0 :(得分: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
.
最后注意事项: 如果输入字符串包含重复的字符,那么将重复一些模式。该解决方案不考虑这种“复杂性”。
答案 1 :(得分:3)
以下是COBOL中Ingo的蓝图(见上文)的实现。我提供它是因为它与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
.
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
.
答案 2 :(得分: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语法是否正确,但您明白了。
答案 3 :(得分:1)
将“值1”固定在第一位,一旦产生24个共同点,就完成循环。
然后,“首先为它创造空间”,将值1放在第2列中。然后在第3列中输入值1.第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排列,除了重新排列之外没有进一步的处理。
为什么没有循环?因为事先已知,所以答案是事先已知的。而不是总是产生相同结果的循环,结果是“硬编码”。
这些节目能否被推广?是。算法是什么?
好吧,您可以描述代码,并找出如何扩展它。或者您可以查看数据。
六对二代的确是做什么的?嗯,两对是两个值的简单排列。六,三个值的排列。做六次四次是四个值的排列。因此,要激活五个值,请将五个单独值中的每一个应用于四个值的置换模式。要激活四个值,请将四个单独值中的每一个应用于三个值的置换模式。要激活三个值,请将三个单独值中的每一个应用于两个值的置换模式。要激活两个值,请将两个单独值中的每一个应用于一个值(!)的置换模式。
因此,为了获得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的“大”尺寸(涉及阶乘,那么“大”很快到达)。
另一件事是“清晰度”。下一个人可以理解代码正在做什么。
如果我能找到时间,我会做一个“漂亮”的版本..,