主题:回溯法:元素的拆分
CLS
INPUT N
LLL = TIMER
DIM a(N)
i = 1
WHILE i > 0
a(i) = a(i) + 1
IF a(i) > N - 1 THEN
a(i) = 0
i = i - 1
ELSE
GOSUB 10
IF i < N AND f = -1 THEN i = i + 1
END IF
WEND
PRINT N; "="; N
PRINT S, TIMER - LLL
END
10
f = -1
FOR j = 1 TO i - 1
IF a(j) > a(i) THEN f = 0: RETURN
NEXT j
k = 0
FOR j = 1 TO i
k = k + a(j)
NEXT j
IF k <> N THEN RETURN
PRINT N; "=";
FOR j = 1 TO i - 1
PRINT a(j); "+";
NEXT j
PRINT a(j)
S = S + 1
RETURN
INPUT N
LLL = TIMER
DIM a(N)
i = 1
WHILE i > 0
a(i) = a(i) + 1
IF a(i) > N - 1 THEN
a(i) = 0
i = i - 1
ELSE
GOSUB 10
IF i < N AND f = -1 THEN i = i + 1
END IF
WEND
PRINT N; "="; N
PRINT S, TIMER - LLL
END
10
f = -1
FOR j = 1 TO i - 1
IF a(j) > a(i) THEN f = 0: RETURN
NEXT j
k = 0
FOR j = 1 TO i
k = k + a(j)
NEXT j
IF k <> N THEN RETURN
PRINT N; "=";
FOR j = 1 TO i - 1
PRINT a(j); "+";
NEXT j
PRINT a(j)
S = S + 1
RETURN