B 排序去重(水题)

问题描述
用计算机生成了N个1到1000之间的随机整数(0¡N≤1000),对于其中重复的数字,只保留一个,
把其余相同的数去掉,然后再把这些数从小到大排序。
输入
有2行,第1行为1个正整数,表示所生成的随机数的个数:
N
第2行有N个用空格隔开的正整数,为所产生的随机数。
(多组输入,输入至文件结束)
输出
也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整
数,为从小到大排好序的不相同的随机数
输入样例
15
5 8 55 2 6 4 7 56 47 18 222 546 254 56 32
输出样例
14
2 4 5 6 7 8 18 32 47 55 56 222 254 546

#include<stdio.h>
#include<iostream>
#include<set>
using namespace std;
int main()
{
	set<int >s;
	set<int > :: iterator it;
	int n,j;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			int x;
			scanf("%d",&x);
			s.insert(x);
		}
		j=s.size();
		cout<<j<<endl;
		for(it=s.begin();it!=s.end();it++)
		{
			printf("%d ",*it);
		}
		cout<<endl;
		s.clear();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yuebaba/article/details/81162598