Заполнить массив змейкой- QBasic(Бейсик)

7 13 14 16
6 8 12 15
2 5 9 11
1 3 4 10
INPUT "vvedite razmer N ", n
DIM a(n, n)
i = n
j = 1
s = -1
DO
    DO
        k = k + 1
        a(i, j) = k
        i = i + s
        j = j + s
    LOOP UNTIL i > n OR j > n OR i < 1 OR j < 1
    IF i < 1 AND j < 1 THEN i = 1: j = 2
    IF i > n AND j > n THEN i = n - 1: j = n
    IF j < 1 THEN j = 1
    IF i < 1 THEN i = 1: j = j + 2
    IF i > n THEN i = n:
    IF j > n THEN i = i - 2: j = n
    s = -s
LOOP UNTIL k = n * n
PRINT "massiv"
FOR i = 1 TO n
    FOR j = 1 TO n
        PRINT a(i, j);
    NEXT
    PRINT
NEXT

Тестирование выполнено в программе QB64 ( Скачать )

Leave a Comment