HDOJ 2034 求差集

首先要搞明白差集的定义,A-B的结果是A中有而B中没有的所有元素的集合。

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int m, n;
	while(cin >> m >> n && (m || n)) {
		set<int> s;
		int a, b;
		while(m--) {
			cin >> a;
			s.insert(a);
		}
		while(n--) {
			cin >> b;
			if(s.count(b)) s.erase(b);
		}
		if(s.empty()) cout << "NULL";
		else	for(const int i : s)
					cout << i << " ";
			// set<int>::iterator it = s.begin();
			// while(it != s.end()) {
			// 	cout << *it << " ";
			// 	it++;
			//}
		cout << endl;
	}
	return 0;
}
/**/

然后就是用set即可。

输出的用法值得深思并记忆。。

猜你喜欢

转载自blog.csdn.net/wen__han/article/details/82253938
今日推荐