主题:[讨论]一程序求解
木偶之舞
[专家分:60] 发布于 2008-04-30 18:36:00
编一程序,自选9个数,把它们存放二维数组A中,并把这个数组的元素按列的上升顺序排列。
如:
排列前: 5 7 3
2 1 6
8 9 4
排列后: 1 4 7
2 5 8
3 6 9
回复列表 (共1个回复)
沙发
Mato完整版 [专家分:1270] 发布于 2008-05-01 17:51:00
DECLARE SUB sort (s%(), k%)
OPTION BASE 1
RANDOMIZE TIMER
DEFINT A-Z
CLS
DIM a(3, 3), b(9)
FOR i = 1 TO 3
FOR j = 1 TO 3
s = INT(RND * 100) + 1
a(i, j) = s
b((i - 1) * 3 + j) = s
NEXT j, i
CALL sort(b(), 9)
FOR i = 1 TO 9
ss = (i = 3 OR i = 6 OR i = 9)
a(i MOD 3 - ss * 3, i \ 3 + 1 + ss) = b(i)
NEXT i
FOR i = 1 TO 3: FOR j = 1 TO 3
PRINT USING "####"; a(i, j);
NEXT j: PRINT : NEXT i: END
SUB sort (s(), k)
IF k = 1 THEN EXIT SUB
IF k = 2 THEN
IF s(1) > s(2) THEN SWAP s(1), s(2)
EXIT SUB
END IF
t = INT(k * .5)
DIM p1(1 TO t), p2(1 TO k - t)
FOR i = 1 TO t: p1(i) = s(i): NEXT i
FOR i = t + 1 TO k: p2(i - t) = s(i): NEXT i
CALL sort(p1(), t)
CALL sort(p2(), k - t)
x = 1: y = 1: sss = 0
DO
sss = sss + 1
IF p1(x) < p2(y) THEN s(sss) = p1(x): x = x + 1 ELSE s(sss) = p2(y): y = y + 1
LOOP UNTIL x > t OR y > k - t
IF x > t THEN
FOR i = y TO k - t: s(sss + 1 - y + i) = p2(i): NEXT i
ELSE
FOR i = x TO t: s(sss + 1 - x + i) = p1(i): NEXT i
END IF
END SUB
我来回复