set 20pts 求助
查看原帖
set 20pts 求助
1120928
XuYu0317楼主2024/3/28 19:48

想用set做这道题,但是set是有序的,与题目要求不符,有没有一种方法可以用set并且保证顺序不变?

#include <iostream>
#include <cstdio>
#include <set>

using namespace std;
int n, m, lena, lenb;
set<int> a, b;

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1, d; i <= n; i ++) { // 读入a
		scanf("%d", &d);
		a.insert(d);
	}

	for (int i = 1, d; i <= m; i ++) { // 读入b
		scanf("%d", &d);
		b.insert(d);
	}

	for (const auto& thi : a) { // 遍历a,与b进行比对
		if (b.count(thi)) printf("%d ", thi);
	}
	return 0;
}
2024/3/28 19:48
加载中...