【洛谷】明明的随机数(双指针去除重复元素)
题目描述

代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int A[n];
for(int i = 0; i < n; i++){
cin>>A[i];
}
sort(A,A+n);
int slow = 0,fast = 0;
while(fast < n){
if(slow != fast){
A[slow] = A[fast];
}
if(fast == n-1){
break;
}
if(A[fast + 1] != A[fast]){
slow++;
}
fast++;
}
cout<<slow+1<<endl;
for(int i = 0; i <= slow; i++){
cout<<A[i]<<" ";
}
return 0;
}

浙公网安备 33010602011771号