[NOIP2006 Popularization Group] Obviously random numbers

Topic link

The topic description
clearly wants to invite some students to do a questionnaire in the school. For the objectivity of the experiment, he first generated N random integers between 1 and 1000 (N≤100) for the repeated numbers. , Keep only one, remove the rest of the same number, different numbers correspond to different student numbers. Then sort these numbers from smallest to largest, and go to classmates to investigate in the order. Please help clearly complete the work of "de-duplication" and "sorting".

Input format
There are two lines of input, the first line is a positive integer, which represents the number of random numbers generated N

There are N positive integers separated by spaces in the second line, which are the random numbers generated.

Output format The
output is also two lines, the first line is a positive integer M, which represents the number of different random numbers.

The second line is M positive integers separated by spaces, which are different random numbers sorted from small to large.

Code:

//P1059 明明的随机数
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    
    
	int n, m;
	vector<int> v;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
    
    
		cin >> m;
		v.push_back(m);
	}
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	cout << v.size() << endl;
	for(int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113738543
Recommended