AcWing 817. 数组去重
原题链接
中等
作者:
浮玺君
,
2021-02-01 11:47:04
,
所有人可见
,
阅读 319
题目描述
样例
算法1
(暴力枚举)
#include<iostream>
using namespace std;
int a[1010];
bool b[1010];
int get_unique_count(int a[], int n)
{
//简单粗暴做法
int cnt = 0;
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(a[i] == a[j])
{
int t = j + 1;
for( ; t < n ; t++)
{
a[t - 1] = a[t];
}
//将最后一个元素去除 整体长度减一
j --;
n --;
}
}
}
//现在的数组总长度 就是去重后的数组长度
cnt = n;
return cnt;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++ ) cin >> a[i];
cout << get_unique_count(a,n);
return 0;
}