主题:[讨论]螺旋方阵
tz_xinzhe
[专家分:200] 发布于 2007-07-15 14:20:00
贵求1在n列n行逆时针旋转的螺旋方阵如何编写?
回复列表 (共7个回复)
沙发
Matodied [专家分:7560] 发布于 2007-07-15 15:53:00
加分!
CLS
INPUT n
IF n = 1 THEN PRINT 1: END
DIM a(n + 1, n + 1)
s = 1: i = n: j = n: k = n
DO
DO WHILE (a(i, j) = 0) AND (i >= 1)
a(i, j) = s
IF s = n * n THEN EXIT DO
i = i - 1: s = s + 1
LOOP
i = i + 1: j = j - 1
DO WHILE (a(i, j) = 0) AND (j >= 1)
a(i, j) = s
IF s = n * n THEN EXIT DO
j = j - 1: s = s + 1
LOOP
j = j + 1: i = i + 1
DO WHILE (a(i, j) = 0) AND (i <= k)
a(i, j) = s
IF s = n * n THEN EXIT DO
i = i + 1: s = s + 1
LOOP
i = i - 1: j = j + 1
DO WHILE (a(i, j) = 0) AND (j <= k)
a(i, j) = s
IF s = n * n THEN EXIT DO
j = j + 1: s = s + 1
LOOP
j = j - 1: i = i - 1: k = k - 1
LOOP UNTIL s = n * n
IF n MOD 2 = 1 THEN a(i, j) = s
FOR i = 1 TO n: FOR j = 1 TO n
PRINT USING "####"; a(i, j);
NEXT j: PRINT : NEXT i: END
板凳
tz_xinzhe [专家分:200] 发布于 2007-07-16 22:09:00
请各位大虾帮帮忙,1在n行n列的回旋方阵(逆时针),不是书上方法.要是正确,小弟我必加分
3 楼
moz [专家分:37620] 发布于 2007-07-17 12:12:00
[url]http://www.programfan.com/club/post-119249-2.html[/url]
DEFINT A-Z
INPUT a
b = a * 2
d = a
c = 1 - d
DIM h(a * a)
FOR i = b TO 2 STEP -1
FOR j = 1 TO i \ 2
c = c + d
g = g + 1
h(c) = g
NEXT
d = (a + 1 - ABS(d)) * (d \ a - d MOD a)
NEXT
FOR i = 1 TO a * a
PRINT USING "###"; h(i);
IF i MOD a = 0 THEN PRINT
NEXT
4 楼
Matodied [专家分:7560] 发布于 2007-07-17 13:20:00
看我的。
CLS
INPUT n
DIM a(n, n)
s = 1
k = n - 1
p = 0
DO
p = p + 1: a(n + 1 - s, n + 1 - s) = p
IF k = 0 THEN EXIT DO
FOR i = 1 TO k
p = p + 1: a(n + 1 - s - i, n + 1 - s) = p
NEXT i
FOR i = 1 TO k
p = p + 1: a(s, n + 1 - s - i) = p
NEXT i
FOR i = 1 TO k
p = p + 1: a(i + s, s) = p
NEXT i
k = k - 1
IF k = 0 THEN EXIT DO
FOR i = 1 TO k
p = p + 1: a(n + 1 - s, i + s) = p
NEXT i
s = s + 1: k = k - 1
LOOP
FOR i = 1 TO n: FOR j = 1 TO n
PRINT USING "####"; a(i, j);
NEXT j: PRINT : NEXT i: END
加分!
5 楼
Lovely哆啦 [专家分:1360] 发布于 2007-07-20 09:22:00
CLS
INPUT N
DIM A(N, N)
FOR I = 1 TO 4: READ X1(I), Y1(I), X2(I), Y2(I): NEXT I
DATA 1,0,1,-1,0,1,1,1,-1,0,-1,1,0,-1,0,0
Y = 1
FOR I = N - 1 TO 0 STEP -2
IF I = 0 THEN A(X + 1, Y) = N * N: GOTO 10
FOR J = 1 TO 4
FOR K = 1 TO I
X = X + X1(J): Y = Y + Y1(J)
S = S + 1
A(X, Y) = S
NEXT K
X = X + X2(J): Y = Y + Y2(J)
NEXT J
NEXT I
10 FOR I = 1 TO N
FOR J = 1 TO N
PRINT USING "####"; A(I, J);
NEXT J: PRINT
NEXT I
END
我把四角螺旋给你了,只是不知道你是要四角螺旋,还是三角螺旋,还是其他.
6 楼
Lovely哆啦 [专家分:1360] 发布于 2007-07-23 12:43:00
CLS
INPUT n
DIM a(n, n)
s = 1
k = n - 1
p = 0
DO
p = p + 1: a(n + 1 - s, n + 1 - s) = p
IF k = 0 THEN EXIT DO
FOR i = 1 TO k
p = p + 1: a(n + 1 - s - i, n + 1 - s) = p
NEXT i
FOR i = 1 TO k
p = p + 1: a(s, n + 1 - s - i) = p
NEXT i
FOR i = 1 TO k
p = p + 1: a(i + s, s) = p
NEXT i
k = k - 1
IF k = 0 THEN EXIT DO
FOR i = 1 TO k
p = p + 1: a(n + 1 - s, i + s) = p
NEXT i
s = s + 1: k = k - 1
LOOP
FOR i = 1 TO n: FOR j = 1 TO n
PRINT USING "####"; a(i, j);
NEXT j: PRINT : NEXT i: END
加分!
7 楼
imjohnzj [专家分:1490] 发布于 2008-10-28 23:12:00
我也写了段代码:
CLS
DIM t
FOR y = -3 TO 3
FOR x = -3 TO 3
IF x = 0 AND y = 0 THEN
PRINT USING "####"; 1;
ELSEIF x >= y THEN
IF x + y > 0 THEN t = (x - 1) * 2 + 1 ELSE t = y * 2 - 1
t = t * t
PRINT USING "####"; t + x + y;
ELSE
IF x + y > 0 THEN t = y * 2 ELSE t = x * 2
t = t * t
t = t + 1
PRINT USING "####"; t - x - y;
END IF
NEXT x
PRINT
NEXT y
看起来好像是我的代码不用数组,代码又短的了。
我来回复