Упорядочить по убыванию все элементы массива X(N), принадлежащие одновременно и массиву Y(N)- QBasic(Бейсик)

DATA 2,4,6,8,10
DATA 1,2,3,8,6
CLS
CONST n = 5
DIM X(n), Y(n), Z(n), cnt

FOR i = 1 TO n
    READ X(i)
NEXT
FOR i = 1 TO n
    READ Y(i)
NEXT

' Sozdanie novogo massiva prinadlezhashchikh elementov
FOR iy = 1 TO n: FOR ix = 1 TO n
        IF X(iy) = Y(ix) THEN cnt = cnt + 1: Z(cnt) = X(iy)
NEXT: NEXT

' Sortirovka
FOR iy = cnt - 1 TO 1 STEP -1: FOR ix = iy TO cnt - 1
        IF Z(ix + 1) > Z(ix) THEN SWAP Z(ix), Z(ix + 1)
NEXT ix, iy

' Vivod
FOR i = 1 TO cnt
    PRINT Z(i)
NEXT

Следующий вариант

10 CLS
PRINT "VVEDITE 555 DLYA VYHODA"
INPUT "Elementov v masive:", n
IF n = 555 THEN END
IF n < 1 GOTO 10
DIM X(n), Y(n), Z(n), nm(n), cnt

RANDOMIZE TIMER
PRINT "Elem X:";
FOR i = 1 TO n
    X(i) = INT(RND * 10)
    Y(i) = INT(RND * 10)
    PRINT X(i);
NEXT: PRINT CHR$(13) + "Elem Y:";
FOR i = 1 TO n
    PRINT Y(i);
NEXT: PRINT CHR$(13)

' Sozdanie novogo massiva prinadlezhashchikh elementov
FOR iy = 1 TO n: FOR ix = 1 TO n
        IF X(iy) = Y(ix) AND nm(iy) = 0 THEN cnt = cnt + 1: Z(cnt) = X(iy): nm(iy) = 1
NEXT: NEXT

IF cnt > 1 THEN
    ' Sortirovka
    FOR iy = cnt - 1 TO 1 STEP -1: FOR ix = iy TO cnt - 1
            IF Z(ix + 1) > Z(ix) THEN SWAP Z(ix), Z(ix + 1)
    NEXT ix, iy
END IF

' Vivod
IF cnt > 0 THEN
    PRINT "Sortirovannye elem prinadlejashye Y:"
    FOR i = 1 TO cnt
        PRINT Z(i);
    NEXT
ELSE: PRINT "Netu prinadlezashyh elementov!!!"
END IF

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

Leave a Comment