6-1 Interval deletion of array elements

6-1 Interval deletion of array elements (20 points)

Given a linear table stored sequentially, please design a function to delete all elements whose value is greater than min and less than max. After deletion, the remaining elements in the table are stored in order, and the relative position cannot be changed.

Function interface definition:

int Delete (int A [], int L, int minA, int maxA);

Among them, A is an integer array, which stores the elements of the original linear table; L is the length of the table, that is, the number of elements in A; minA and maxA are the lower and upper bounds of the value range of the elements to be deleted. The function Delete should delete all elements in A whose value is greater than minA and less than maxA, while ensuring that the remaining elements in the table are stored in order and the relative position is unchanged, and finally returns the deleted table length.

Sample referee test procedure:

#include <stdio.h>

#define MAXN 20

int Delete( int A[], int L, int minA, int maxA );

int main()
{
    
    
    int A[MAXN], L, minA, maxA, i;

    scanf("%d", &L);
    for (i=0; i<L; i++) scanf("%d", &A[i]);
    scanf("%d %d", &minA, &maxA);
    L = Delete(A, L, minA, maxA);
    for (i=0; i<L; i++) printf("%d ", A[i]);
    printf("\n");

    return 0;
}

/* Your code will be embedded here */

Input sample:

10
4 -8 2 12 1 5 9 3 3 10
0 4

Sample output:

4 -8 12 5 9 10

int Delete( int A[], int L, int minA, int maxA )
{
    
    
	int cnt=0;
	int a[L];
	for(int i=0;i<L;i++)
	{
    
    
		if(!(A[i]>minA&&A[i]<maxA))
		{
    
    
			a[cnt] = A[i] ;
			cnt++;
		}
	}
	for(int i=0;i<cnt;i++)
	{
    
    
		A[i] = a[i];
	}
	return cnt;
}
int Delete( int A[], int L, int minA, int maxA )
{
    
    
	int a = -1 ;
	for(int i=0;i<L;i++)
	{
    
    
		if(!(A[i]>minA&&A[i]<maxA))
		{
    
    
			a++;
			A[a] = A[i]; 
		}
	}	
	return a+1;
}

The first is to create an auxiliary array to solve the problem, but it is a waste of space

The second is a simplified board for fast and slow pointers

Guess you like

Origin blog.csdn.net/weixin_51198300/article/details/114534862