문제 링크: https://www.acmicpc.net/problem/1269
백준 집합과 맵 6단계 - 1269번 대칭 차집합을 풀어보았다.
풀이: 집합 두 개를 서로 빼서 남은 요소의 수를 구한다.
C++의 경우, set_symmetric_difference를 이용했는데, 결과를 넣는 곳인 back_inserter는 벡터를 써야 하는 거 같다. 다른 사람들도 다 벡터를 쓰길래 set을 입력해 봤더니 에러가 났다.
C++
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
set<int> a;
set<int> b;
vector<int> res;
for(int i=0;i<n;i++)
{
int nn;
cin>>nn;
a.insert(nn);
}
for(int i=0;i<m;i++)
{
int nn;
cin>>nn;
b.insert(nn);
}
set_symmetric_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(res)); // back_inserter를 꼭 써줘야하는거 같다
cout<<res.size();
}
Python
n,m=map(int,input().split())
sn=set(map(int,input().split()))
sm=set(map(int,input().split()))
print(len(sn-sm)+len(sm-sn))
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
직사각형에서 탈출 풀이 (0) | 2023.01.09 |
---|---|
서로 다른 부분 문자열의 개수 풀이 (0) | 2023.01.09 |
듣보잡 풀이 (0) | 2023.01.06 |
숫자 카드 2 풀이 (0) | 2023.01.06 |
나는야 포켓몬 마스터 이다솜 풀이 (0) | 2023.01.06 |