主题:!!!问题!!!
bond007
[专家分:130] 发布于 2007-08-01 20:29:00
从键盘输入四个自然数,请用这四个自然数组成一个不含 括号的加减法混合运算题,使该题所得结果最小(但不能为负值,而且运算的中间结果也不能为负值)。输出格式要符合数学算式的要求,如有多种解答,需输出所有符合要求的表达式
如:输入: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
有没有简单方法!
最后更新于:2007-08-01 20:37:00
回复列表 (共9个回复)
沙发
bond007 [专家分:130] 发布于 2007-08-01 20:39:00
板凳
wzc1996 [专家分:1680] 发布于 2007-08-01 20:39:00
CLS
DATA 1,1,1,1,1,-1,1,-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,-1,-1
DIM a(4), b(4), f(3)
INPUT a(1), a(2), a(3), a(4): t = 10000
FOR i = 1 TO 4: FOR j = 1 TO 4: FOR k = 1 TO 4: FOR l = 1 TO 4
IF i <> j AND i <> k AND i <> l AND j <> k AND j <> l AND k <> l THEN
s = 0: RESTORE
FOR m = 1 TO 8
READ f1, f2, f3
s = a(i)
s = s + f1 * a(j): IF s < 0 THEN 1
s = s + f2 * a(k): IF s < 0 THEN 1
s = s + f3 * a(l): IF s < 0 THEN 1
IF s < t THEN
t = s: b(1) = a(i): b(2) = a(j): b(3) = a(k): b(4) = a(l)
f(1) = f1: f(2) = f2: f(3) = f3
END IF
1 NEXT m
END IF
NEXT l, k, j, i
FOR i = 1 TO 3
IF f(i) = 1 THEN s$ = "+" ELSE s$ = "-"
PRINT b(i); s$;
NEXT i
PRINT b(4); "="; t: END
3 楼
wzc1996 [专家分:1680] 发布于 2007-08-01 20:51:00
min = 10000
CLS
INPUT a, b, c, d
DIM jg(10000)
a(1) = a
a(2) = b
a(3) = c
a(4) = d
F$(1) = "+"
F$(2) = "-"
FOR a1 = 1 TO 4
FOR a2 = 1 TO 4
FOR a3 = 1 TO 4
FOR a4 = 1 TO 4
FOR f1 = 1 TO 2
FOR f2 = 1 TO 2
FOR f3 = 1 TO 2
IF a1 * a2 * a3 * a4 = 24 AND a1 + a2 + a3 + a4 = 10 THEN
IF f1 = 1 THEN jg = a(a1) + a(a2) ELSE jg = a(a1) - a(a2)
IF jg >= 0 THEN IF f2 = 1 THEN jg = jg + a(a3) ELSE jg = jg - a(a3)
IF jg >= 0 THEN IF f3 = 1 THEN jg = jg + a(a4) ELSE jg = jg - a(a4)
IF jg < min AND jg >= 0 THEN
min = jg
b(1) = a(a1)
b(2) = a(a2)
b(3) = a(a3)
b(4) = a(a4)
F(1) = f1
F(2) = f2
F(3) = f3
jg = 0
END IF
END IF
NEXT f3
NEXT f2
NEXT f1
NEXT a4
NEXT a3
NEXT a2
NEXT a1
FOR I = 1 TO 3
IF F(I) = 1 THEN PRINT b(I); "+"; ELSE PRINT b(I); "-";
NEXT
PRINT b(4); "="; min
END
4 楼
Matodied [专家分:7560] 发布于 2007-08-01 20:57:00
楼上的又是抄我的。
以后我们就叫wzc1996“抄袭大王”吧!
其实这个程序根本就是错的。
正确的:
CLS
DATA 1,1,1,1,1,-1,1,-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,-1,-1
DIM a(4), b(4), f(3)
INPUT a(1), a(2), a(3), a(4): t = 10000
FOR i = 1 TO 4: FOR j = 1 TO 4: FOR k = 1 TO 4: FOR l = 1 TO 4
IF i <> j AND i <> k AND i <> l AND j <> k AND j <> l AND k <> l THEN
s = 0: RESTORE
FOR m = 1 TO 8
READ f1, f2, f3
s = a(i)
s = s + f1 * a(j): IF s < 0 THEN 1
s = s + f2 * a(k): IF s < 0 THEN 1
s = s + f3 * a(l): IF s < 0 THEN 1
IF s < t THEN t = s
1 NEXT m
END IF
NEXT l, k, j, i
FOR i = 1 TO 4: FOR j = 1 TO 4: FOR k = 1 TO 4: FOR l = 1 TO 4
IF i <> j AND j <> k AND k <> l AND i <> k AND i <> l AND j <> l THEN
s = 0: RESTORE
FOR m = 1 TO 8
READ f1, f2, f3
s = a(i)
s = s + f1 * a(j): IF s < 0 THEN 2
s = s + f2 * a(k): IF s < 0 THEN 2
s = s + f3 * a(l): IF s < 0 THEN 2
IF s = t THEN GOSUB pri
2 NEXT m
END IF
NEXT l, k, j, i
END
pri:
PRINT a(i);
IF f1 = 1 THEN s$ = "+" ELSE s$ = "-"
PRINT s$; a(j);
IF f2 = 1 THEN s$ = "+" ELSE s$ = "-"
PRINT s$; a(k);
IF f3 = 1 THEN s$ = "+" ELSE s$ = "-"
PRINT s$; a(l); "="; t
RETURN
5 楼
Matodied [专家分:7560] 发布于 2007-08-01 21:13:00
解释一下我的程序:
第一步先枚举运算符,1表示+,-1表示-。然后弄4个循环,分别表示这4个数在算式里的位置,但不能相同,在不相同的情况下,用8次循环表示所有可能的运算符(共有8种:+、+、+,+、+、-,+、-、+,+、-、-,-、+、+,-、+、-,-、-、+,-、-、-),接着是做运算,做运算时要注意:只要出现了负数就表示此次运算不成功,不能计入,如果每次运算结果都是正数,那么再判断目前的结果是不是小于当前的最小结果t(由于最小值不知道,这里t的初值可以取一个比较大的数,如10000),如果小于,那么令t=s,所有的可能都枚举完毕后,t就是最小正数结果。
由于结果是t的算式可能不止一个,因此必须再做一次枚举。这次枚举的过程和第一步差不多,只是最后判断的条件是:凡是运算结果等于t的,就输出,最后就可输出所有算式。
6 楼
wzc1996 [专家分:1680] 发布于 2007-08-01 21:18:00
2 楼 是参考你的,有人不会,拿出来,有什么不对的!
3 楼 不是抄的,是自己的!
你可真无耻,卑鄙!
小心我告你诽谤!
7 楼
YF99_66 [专家分:30] 发布于 2007-08-02 08:31:00
8 楼
yfyf [专家分:440] 发布于 2007-08-02 08:33:00
CLS
DATA 1,1,1,1,1,-1,1,-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,-1,-1
DIM a(4), b(4), f(3)
INPUT a(1), a(2), a(3), a(4): t = 10000
FOR i = 1 TO 4: FOR j = 1 TO 4: FOR k = 1 TO 4: FOR l = 1 TO 4
IF i <> j AND i <> k AND i <> l AND j <> k AND j <> l AND k <> l THEN
s = 0: RESTORE
FOR m = 1 TO 8
READ f1, f2, f3
s = a(i)
s = s + f1 * a(j): IF s < 0 THEN 1
s = s + f2 * a(k): IF s < 0 THEN 1
s = s + f3 * a(l): IF s < 0 THEN 1
IF s < t THEN
t = s: b(1) = a(i): b(2) = a(j): b(3) = a(k): b(4) = a(l)
f(1) = f1: f(2) = f2: f(3) = f3
END IF
1 NEXT m
END IF
NEXT l, k, j, i
FOR i = 1 TO 3
IF f(i) = 1 THEN s$ = "+" ELSE s$ = "-"
PRINT b(i); s$;
NEXT i
PRINT b(4); "="; t: END
9 楼
Lovely哆啦 [专家分:1360] 发布于 2007-09-28 21:44:00
你可瞒不了我的!
我来回复