洛谷歷險記 p1059

其實這個題目很簡單,主要是我用的方法比較有意思

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>

using namespace std;


int main() {
	int N;
	cin >> N;
	set<int> nums;
	int temp;
	for (int i = 0; i < N; i++)
	{
		cin >> temp;
		nums.insert(temp);
	}
	set<int>::iterator it=nums.begin();
	cout << nums.size() << endl;
	while (it != nums.end()) {
		cout << *it<<" ";
		it++;
	}
	return 0;
}

set的特性是裏面的元素不能重複,完美解決題目的要求,而且對set的插入還能保持增序

然後我用了迭代器,來展示set裏的內容

迭代器的用法:http://c.biancheng.net/view/338.html

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章