主题:[原创]抛砖引玉之二:螺旋矩阵--我写出了更好的方法
flyinggoon
[专家分:110] 发布于 2008-05-23 13:21:00
'完善程序,框架确定好了,不让从四周构造,必须从中心开始。哪怕网友有更简便、更通用的办法?请指导为盼!
CLS : INPUT n: DIM a(n, n): k = 0
IF n MOD 2 = 1 THEN k = k + 1: a(n \ 2 + 1, n \ 2 + 1) = k
FOR i = 1 TO n \ 2
FOR j = n \ 2 + i TO n \ 2 + 2 - i STEP -1
k = k + 1: a((n + 1) \ 2 + i, j) = k
NEXT j
FOR j = (n + 1) \ 2 + i TO n \ 2 + 2 - i STEP -1
k = k + 1: a(j, n \ 2 + 1 - i) = k
NEXT j
FOR j = n \ 2 + 1 - i TO n \ 2 + i
k = k + 1: a(n \ 2 + 1 - i, j) = k
NEXT j
IF k < n * n THEN
FOR j = n \ 2 + 1 - i TO (n + 1) \ 2 + i
k = k + 1: a(j, n \ 2 + 1 + i) = k
NEXT j
END IF
NEXT i
FOR i = 1 TO n
FOR j = 1 TO n
PRINT USING "###"; a(i, j);
NEXT j: PRINT
NEXT i
END
'新方法
CLS : INPUT n: DIM a(n, n): k = 0
FOR i = 1 TO (n + 1) \ 2
FOR j = n + 1 - i TO i STEP -1
k = k + 1: a(j, n + 1 - i) = n * n + 1 - k
NEXT j '下
FOR j = n - i TO i STEP -1
k = k + 1: a(i, j) = n * n + 1 - k
NEXT j '左
FOR j = i + 1 TO n + 1 - i
k = k + 1: a(j, i) = n * n + 1 - k
NEXT j '上
FOR j = i + 1 TO n - i
k = k + 1: a(n + 1 - i, j) = n * n + 1 - k
NEXT j
NEXT i
FOR i = 1 TO n
FOR j = 1 TO n
PRINT USING "###"; a(i, j);
NEXT j: PRINT
NEXT i
END
最后更新于:2008-05-25 20:34:00
回复列表 (共11个回复)
沙发
冷石_jasv [专家分:1570] 发布于 2008-05-23 20:25:00
哈哈。你们有没有发现。有点象老虎机游戏显示亮灯的原理.
板凳
fanyangjie [专家分:20] 发布于 2008-05-26 20:34:00
这段程序有漏洞,没有考虑n的奇偶
3 楼
flyinggoon [专家分:110] 发布于 2008-05-27 16:57:00
你说对了!加分。
因为两种方法写在了一起了,所以,我就把
IF n MOD 2 = 1 THEN k = k + 1: a(n \ 2 + 1, n \ 2 + 1) = k
一句没复制下来。
4 楼
崇禎十八年 [专家分:10] 发布于 2008-06-01 20:06:00
妳有沒有覺得這個程序有點煩,還不如原來的程序思路清晰。
5 楼
flyinggoon [专家分:110] 发布于 2008-06-04 16:45:00
怎么会呢?原来的程序有if语句限制,而后面的程序更通用,不需加if语句
6 楼
fanyangjie [专家分:20] 发布于 2008-06-21 16:48:00
是新方法好,我们考试也曾考过这一题,新方法是分层的方便
7 楼
def [专家分:3380] 发布于 2008-07-13 15:03:00
一个寻欢+判断改编变量(x,y,tx,ty,n)+tx+=x,ty+=y
(我不会)
8 楼
def [专家分:3380] 发布于 2008-07-13 15:04:00
我的alittledifficult
9 楼
phile [专家分:2310] 发布于 2009-06-30 17:39:00
也没改进多少啊?!
10 楼
phile [专家分:2310] 发布于 2009-06-30 17:47:00
其实不必这样
可以从25倒着转
如
CLS
INPUT N
DIM A(N, N)
K = N * N
FOR I = 1 TO N \ 2 + 1
FOR J = N + 1 - I TO I STEP -1
A(J, N + 1 - I) = K
K = K - 1
NEXT J
FOR J = N - I TO I STEP -1
A(I, J) = K
K = K - 1
NEXT J
FOR J = I + 1 TO N + 1 - I
A(J, I) = K
K = K - 1
NEXT J
FOR J = I + 1 TO N - I
A(N + 1 - I, J) = K
K = K - 1
NEXT J
NEXT I
FOR I = 1 TO N
FOR J = 1 TO N
PRINT USING "###"; A(I, J);
NEXT J
PRINT
NEXT I
我来回复