Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Flutter
- baekjoon
- python
- 백준
- Spring Security
- springboot
- Oracle
- 플러터
- postgresql
- CS
- 네트워크
- Java
- 스프링부트
- programmers
- 리눅스
- 스프링
- 프로그래머스
- CS지식
- 파이썬
- 스프링 부트 쇼핑몰 프로젝트 with JPA
- DB
- backjoon
- 데이터베이스
- 시큐리티
- JPA
- 데이터
- spring
- 자료구조
- 자바스크립트
- 자바
Archives
- Today
- Total
Jin's Dev Story
[실버 4] 1269번 대칭 차집합 본문
문제 링크 : https://www.acmicpc.net/problem/1269
문제
자연수를 원소로 갖는 공집합이 아닌 두 집합 A와 B가 있다. 이때, 두 집합의 대칭 차집합의 원소의 개수를 출력하는 프로그램을 작성하시오. 두 집합 A와 B가 있을 때, (A-B)와 (B-A)의 합집합을 A와 B의 대칭 차집합이라고 한다.
예를 들어, A = { 1, 2, 4 } 이고, B = { 2, 3, 4, 5, 6 } 라고 할 때, A-B = { 1 } 이고, B-A = { 3, 5, 6 } 이므로, 대칭 차집합의 원소의 개수는 1 + 3 = 4개이다.
입력
첫째 줄에 집합 A의 원소의 개수와 집합 B의 원소의 개수가 빈 칸을 사이에 두고 주어진다. 둘째 줄에는 집합 A의 모든 원소가, 셋째 줄에는 집합 B의 모든 원소가 빈 칸을 사이에 두고 각각 주어진다. 각 집합의 원소의 개수는 200,000을 넘지 않으며, 모든 원소의 값은 100,000,000을 넘지 않는다.
출력
첫째 줄에 대칭 차집합의 원소의 개수를 출력한다.
예제 입력 1
3 5
1 2 4
2 3 4 5 6
예제 출력 1 : 4
코드
import java.io.*;
import java.util.*;
public class num1269 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int aLen = Integer.parseInt(st.nextToken()); // A의 개수
int bLen = Integer.parseInt(st.nextToken()); // B의 개수
Set<Integer> aSet = new HashSet<Integer>();
st = new StringTokenizer(br.readLine());
for(int i=0; i<aLen; i++) aSet.add(Integer.parseInt(st.nextToken()));
Set<Integer> bSet = new HashSet<Integer>();
st = new StringTokenizer(br.readLine());
for(int i=0; i<bLen; i++) bSet.add(Integer.parseInt(st.nextToken()));
int count = 0;
for(int num : aSet)
if(!bSet.contains(num)) count++;
for(int num : bSet)
if(!aSet.contains(num)) count++;
System.out.println(count);
}
}
'Coding Test > 백준[JAVA]' 카테고리의 다른 글
[실버 3] 11478번 서로 다른 부분 문자열의 개수 (0) | 2023.09.23 |
---|---|
[실버 4] 1764번 듣보잡 (0) | 2023.09.23 |
[실버 4] 1620번 나는야 포켓몬 마스터 이다솜 (0) | 2023.09.22 |
[실버 4] 10816번 숫자 카드 2 (0) | 2023.09.22 |
[실버 5] 7785번 회사에 있는 사람 (0) | 2023.09.22 |