主题:小学生请求QB高手帮助
问题描述:
输入4个自然数,组成一个不含括号的加法混合运算题,使该题结果最小(但不能为负数,且运算中间的结果也不能为负值)。输出符合要 求的所有表达式。
样例
输入:15,6,12,4
输出:15-12+4-6=1
15+4-12-6=1
15+4-6-12=1
15-6+4-12=1
4+15-12-6=1
4+15-6-12=1
我好不容易编了一个程序,可只能输出一个结果。琢磨半天也看不出错在哪里。求高手指点。
程序如下:
CLS
M = 200
DIM C(M, 4), D(M, 4), H(M)
FOR I = 1 TO 4
INPUT A(I)
B(I) = 0
W(I) = I
S = S + A(I)
NEXT I
MIN = S
B(4) = 1
A$(0) = "+": A$(1) = "-"
S = 1
FOR I = 2 TO 4: S = S * I: NEXT I
FOR I = 1 TO S - 1
J = 4
DO WHILE W(J) <= W(J - 1)
J = J - 1
LOOP
K = 4
DO WHILE W(K) <= W(J - 1)
K = K - 1
LOOP
SWAP W(K), W(J - 1)
FOR P = J TO 3
FOR Q = P + 1 TO 4
IF W(P) > W(Q) THEN SWAP W(P), W(Q)
NEXT Q
NEXT P
DO WHILE B(1) = 0
S = A(W(1))
J = 2
DO WHILE S = ABS(S) AND J <= 4
IF B(J) = 0 THEN S = S + A(W(J)) ELSE S = S - A(W(J))
J = J + 1
LOOP
IF J > 4 AND S = ABS(S) AND S <= MIN THEN
MIN = S
N = N + 1
H(N) = S
FOR J = 1 TO 4
C(N, J) = A(W(J))
D(N, J) = B(J)
NEXT J
END IF
J = 4
DO WHILE B(J) = 1
B(J) = 0
J = J - 1
LOOP
B(J) = 1
LOOP
NEXT I
J = 1
DO WHILE H(J) > MIN
J = J + 1
LOOP
FOR I = J TO N
PRINT C(I, 1);
FOR J = 2 TO 4
PRINT A$(D(I, J)); C(I, J);
NEXT J
PRINT "="; MIN
NEXT I
END