[NOIP 2006 普及组 T1] 明明的随机数
#include <bits/stdc++.h>
using namespace std;
int x[1010];
int main()
{
int n, t, cnt = 0;
bool isfirst = true;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> t;
x[t] = 1;
}
for (int i = 1; i <= 1000; i++)
if (x[i] == 1)
cnt++;
cout << cnt << endl;
for (int i = 1; i <= 1000; i++)
{
if (x[i] == 1)
if (isfirst)
{
cout << i;
isfirst = false;
}
else
cout << ' ' << i;
}
cout << endl;
return 0;
}当然也可以先用sort()函数排序,然后再去重。
#include <bits/stdc++.h>
using namespace std;
long long x[1010];
int main() {
long long n, p = 0, l = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &x[i]);
sort(x, x + n);
for (int i = 0; i < n; i++) {
if (x[i] != x[i + 1])
p++;
}
unique(x, x + n);
cout << p << endl << x[0] << " ";
for (int i = 1; i < p; i++)
printf("%d ", x[i]);
return 0;
}