hdu1412

stl中set的运用,又学到了一个知识点

/*
 * =====================================================================================
 *
 *       Filename:  hd1412.c
 *
 *    Description:  求两个集合的并集,并顺序输出
 *
 *        Version:  1.0
 *        Created:  2013年09月26日 23时37分20秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ni WenXian (hello world~ iam coming), niwenxianq@qq.com
 *   Organization:  AMS/ICT
 *
 * =====================================================================================
 */
 #include <iostream>
 #include <set>
 using namespace std;
 int main(){
	int m,n,x;
	set<int> s;
	set<int>::iterator it;
	while(cin>>m>>n){
		s.clear();
		while(m--){cin>>x;s.insert(x);}
		while(n--){cin>>x;s.insert(x);}
		for(it=s.begin();it!=s.end();it++){
			if(it==s.begin()) cout<<*it;
			else cout<<" "<<*it;
		}
		cout<<endl;
	}
	return 0;
}

如果不用set,自己写起来还真是麻烦. 上面方法还是借用别人的思想

看来处理这类问题,首先想到的应该是stl


方法2:

1.先把所有的数据存储到一个数组中, a[200000]

2.排序a, 不用去重,这样太麻烦,输出的时候作控制就可以了

3. 输出, 如果相同的值,就只输一个

这个还是要用到排序算法

#include <algorithm>

sort(a, a+m+n);

方法3.

直接用

set_intersection(),set_union()就行了




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值