[NOIP 2006 普及组 T1] 明明的随机数

作者: qiqi 分类: CSP-J 发布时间: 2025-10-06 13:28

查看题目

#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;
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

标签云