题目
Petya has an array a consisting of n integers. He wants to remove duplicate (equal) elements.
Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed.
题意
给出一个长度为n的序列,希望你可以去重。
但是去重后留下的数字需要是靠右的。
(“11121”最后留下的应该是“21”)
思路
范围给的很小,所以暴力就好。
看代码吧,伊丽莎白!
代码
#include<bits/stdc++.h>
using namespace std;
int num[1005];
int main()
{
int a[55];
int n;
vector<int>s;
cin>>n;
for(int i=0; i<n; i++)
cin>>a[i];
memset(num,0,sizeof(num));
for(int i=n-1;i>=0;i--)
{
if(!num[a[i]])
{
s.push_back(a[i]);
num[a[i]]++;
}
}
cout<<s.size()<<endl;
for(int i=s.size()-1;i>=0;i--)
{
if(i!=s.size()-1)
cout<<' ';
cout<<s[i];
}
cout<<endl;
}
多做题,终有一日,你也能变成光!