1. 문제

1269번: 대칭 차집합



2. 개요

집합 A, B가 있을 때, A-B 와 B-A의 합집합을 대칭 차집합이라고 한다.

집합 A, B가 주어졌을 때, 대칭 차집합의 원소의 개수를 구하는 문제.



3. 풀이 및 코드

3-1. 풀이

파이썬의 경우 집합간의 -연산을 지원하기 때문에 이를 활용하여 A-B와 B-A 각각의 길이를 더한 값을 출력한다.

C#의 경우 -연산을 지원하지 않기 때문에 Except를 이용해 각각의 차집합을 구한 뒤 더해준다.

3-2. Python

import sys

a, b = map(int, sys.stdin.readline().split())

A = set(list(map(int, sys.stdin.readline().split())))
B = set(list(map(int, sys.stdin.readline().split())))

print(len(A-B) + len(B-A))

3-3. C#

namespace boj_1269
{
    internal class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            string[] input = sr.ReadLine().Split();

            HashSet<int> A = new HashSet<int>(new List<int>(Array.ConvertAll(sr.ReadLine().Split(), int.Parse)));
            HashSet<int> B = new HashSet<int>(new List<int>(Array.ConvertAll(sr.ReadLine().Split(), int.Parse)));

            HashSet<int> C = new HashSet<int>(A.Except(B));
            HashSet<int> D = new HashSet<int>(B.Except(A));

            sw.WriteLine(C.Count + D.Count);
            sw.Close();
        }
    }
}

Comments