DS_T_code_2.2_T5

版权声明:转载请注明出处 https://blog.csdn.net/chushoufengli/article/details/83106775
//顺序表的删除操作_从顺序表中删除在给定值之间的元素
#include <cstdio>
#include <iostream>
using namespace std;
const int MAXN = 1010;
struct l {
	int data[MAXN];
	int length = 0;
} L;
bool solve(l& L, int x_2, int x_3) {
	int k = 0;
	for(int i=0; i<L.length; i++) {
		if(
		(L.data[i] == x_2 || L.data[i] > x_2) && (L.data[i] < x_3 || L.data[i] == x_3)
		) {
			k++;
		} else {
			L.data[i - k] = L.data[i];
		}
	}
	L.length -= k;
	return true;
}
int main() {
	//输入
	scanf("%d",&L.length);
	int x_1;
	for(int i=0; i<L.length; i++) {
		scanf("%d",&x_1);
		L.data[i] = x_1;
	}
	int x_2, x_3;
	scanf("%d",&x_2);
	scanf("%d",&x_3);
	if(x_3 < L.data[0] || x_2 > L.data[L.length - 1]) {
		printf("删除失败");
		return 0;
	}
	if(L.length == 0) {
		printf("删除失败");
		return 0;
	}
	//solve
	if(solve(L, x_2, x_3)) {
		for(int i=0; i<L.length; i++) {
			printf("%d ",L.data[i]);
		}
	} else printf("失败");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/chushoufengli/article/details/83106775
T
今日推荐