PTA: 7-4 Sequence List (Deleted)

Sequence table (delete)

topic

Insert image description here

Input sample

10
55 11 9 15 67 12 18 33 6 22
10 20

Output sample

55 9 67 33 6 22

code

#include<iostream>
using namespace std;
void deletes(int* arr, int& sz, int left, int right)
{
    
    
    int newIdx = 0;
    for (int i = 0; i < sz; ++i)
    {
    
    
        if (arr[i] < left || arr[i] > right)
        {
    
    
            arr[newIdx] = arr[i];
            newIdx++;
        }
    }
    sz = newIdx;
}

int main()
{
    
    
    int num;
    cin >> num;
    int* seq = new int[num];
    for (int i = 0; i < num; ++i)
    {
    
    
        cin >> seq[i];
    }
    int left, right;
    cin >> left;
    cin >> right;

    deletes(seq, num, left, right);

    for (int i = 0; i < num; ++i)
    {
    
    
        cout << seq[i];
        if (i < num - 1)
            cout << " ";
    }

    delete[] seq;
    return 0;
}

① Function deletes: delete elements in the array that are not within the specified range
arr: pointer to the integer array
sz: size of the integer array, update the array size by passing a reference
left: left boundary of the specified range
right: right boundary of the specified range
② Update the array size, Elements not within the specified range were removed
Insert image description here

Guess you like

Origin blog.csdn.net/m0_74195626/article/details/132944439