蓝桥杯 ALGO-111 明明的随机数(排序,去重)

【思路】:vector完美解决。注意用vector的动态数组方式,不然又其他的零。

【AC代码】:

#include 
#include 
#include 
#include 
#include 
using namespace std;

#define MAX 100+5

int cmp(int a, int b)
{
	return a < b;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int n = 0, i = 0;
	//input
	cin >> n;
	vector a(n);
	vector :: iterator Iter;
	for (i = 0; i < n; i++)
		cin >> a[i];
		
	//sort
	sort(a.begin(), a.end(), cmp);
	
	//delete
	for (Iter = a.begin()+1; Iter != a.end(); )
	{
		int c = (*Iter), d = (*(Iter-1));
		if ((*Iter) == (*(Iter-1)))
			Iter = a.erase(Iter);
		else
			Iter++;
	}
	
	//output
	cout << a.size() << endl;
	for (Iter = a.begin(); Iter != a.end(); Iter++)
		cout << (*Iter) << " ";
	return 0;
}


你可能感兴趣的:(基本算法)