以下是引用yangking在2005-11-11 22:05:48的发言:
我以前用QBASIC编过一个,关键是算法,可惜忘记了
10 DIM A(20, 20), A1(20, 20)
20 INPUT N
30 IF N = -1 THEN 230
40 IF N < 3 THEN PRINT "INPUT n AGAIN": GOTO 20
50 N2 = N
60 IF N MOD 4 = 0 THEN GOSUB 470 ELSE 120
70 FOR I = 1 TO N2
80 FOR J = 1 TO N2
90 A1(I, J) = A(I, J)
100 NEXT: NEXT
110 GOTO 150
120 IF N MOD 2 = 0 THEN GOSUB 660 ELSE 140
130 GOTO 150
140 GOSUB 240
150 T = 0
160 FOR I = 1 TO N
170 T = T + A1(I, I)
180 FOR J = 1 TO N
190 PRINT USING "####"; A1(I, J);
200 NEXT: PRINT
210 NEXT: PRINT
220 PRINT "SUM="; T: PRINT : GOTO 20
230 END
240 J = 1: M = N
250 K = (M + 1) / 2
260 M2 = M * M
270 FOR I = 1 TO M2
280 A1(J, K) = I
290 R = I / M
300 B = I - INT(R) * M
310 IF B = 0 THEN 410
320 IF J = 1 THEN 350
330 J = J - 1
340 GOTO 360
350 J = M
360 IF K = M THEN 390
370 K = K + 1
380 GOTO 450
390 K = 1
400 GOTO 450
410 IF J <> M THEN 440
420 J = 1
430 GOTO 450
440 J = J + 1
450 NEXT I
460 RETURN
470 K = 0
480 FOR I = 1 TO N2
490 FOR J = 1 TO N2
500 K = K + 1
510 A(I, J) = K
520 NEXT
530 NEXT
540 N1 = N2 + 1
550 FOR I = 1 TO N2 / 2
560 IF (I + 1) MOD 4 = 0 OR (I + 2) MOD 4 = 0 THEN P = 1 ELSE P = 2
570 FOR J = P TO N2 STEP 4
580 J1 = J + 1
590 SWAP A(I, J), A(N1 - I, N1 - J)
600 IF J = 1 THEN J = 0: GOTO 630
610 IF J = N2 THEN 630
620 SWAP A(I, J1), A(N1 - I, N1 - J1)
630 NEXT
640 NEXT
650 RETURN
660 N2 = N - 2
670 GOSUB 470
680 A0 = (N * N - N2 * N2) / 2
690 FOR I = 1 TO N2
700 FOR J = 1 TO N2
710 A1(I + 1, J + 1) = A(I, J) + A0
720 NEXT
730 NEXT
740 A1(1, 1) = 1: A1(1, N) = 2
750 A1(N, 1) = N * N - 1: A1(N, N) = N * N
760 SUM = N * N + 1
770 IF N = 6 THEN 780 ELSE IF N = 10 THEN 840 ELSE PRINT "INPUT n AGAIN": GOTO 10
780 A1(1, 2) = 9: A1(1, 3) = 34: A1(1, 4) = 33: A1(1, 5) = 32
790 A1(2, 1) = 6: A1(3, 1) = 10: A1(4, 1) = 30: A1(5, 1) = 29
800 FOR I = 2 TO 5
810 A1(N, I) = SUM - A1(1, I): A1(I, N) = SUM - A1(I, 1)
820 NEXT I
830 RETURN
840 A1(1, 2) = 96: A1(1, 3) = 95: A1(1, 4) = 94: A1(1, 5) = 93
850 A1(1, 6) = 90: A1(1, 7) = 9: A1(1, 8) = 10: A1(1, 9) = 15
860 A1(2, 1) = 88: A1(3, 1) = 87: A1(4, 1) = 97: A1(5, 1) = 84
870 A1(6, 1) = 12: A1(7, 1) = 16: A1(8, 1) = 3: A1(9, 1) = 18
880 FOR I = 2 TO 9
890 A1(N, I) = SUM - A1(1, I): A1(I, N) = SUM - A1(I, 1)
900 NEXT I
910 RETURN |