Files
ClassFeedback/C++代码/CSP04/01/小杨有序去重.cpp

37 lines
544 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 100010;
int n;
int arr[MAXN];
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> arr[i];
}
if(n == 0){
cout << endl << 0 << endl;
return 0;
}
int j = 1; // 慢指针
for(int i = 2; i <= n; i++){
// i表示快指针,一直向后移动
if(arr[i] == arr[j]) continue;
// 如果不相同
arr[j+1] = arr[i];
j++;
}
for(int i = 1; i <= j; i++){
cout << arr[i] << " ";
}
cout << endl << j;
return 0;
}