输入时相邻的重复数据去除

这个方法使得输入进去的数,相邻的重复数据去除
#include <bits/stdc++.h>
using namespace std;

int main () {
    
    
	int a[9999];
	int n, x, count = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
    
    
		cin >> x; //用x来替换数组的内容,这样不会有空缺。
		if (x == a[i - 1]) {
    
    
			i--;//使后面的数据消失
			n--;
		}
		a[i] = x;
	}
	for (int j = 0; j < n; j++) {
    
    
		cout << a[j] << ' ';
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_52045928/article/details/113101222