728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
1 처음 풀이


import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int n = sc.nextInt();
boolean[] check = new boolean[10];
int[] focus = new int[4];
int[] arr = new int[4];
int[] cnt = new int[10];
int flag = 0;
for (int i = 0; i < check.length; i++) {
check[i] = true;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < 4; i++) {
arr[i] = sc.nextInt();
}
String isPossible = sc.next();
if (isPossible.equals("YES")) {
for (int i = 0; i < 4; i++) {
if (flag == 0)
focus[i] = arr[i];
cnt[arr[i]]++;
}
flag++;
} else {
for (int i = 0; i < 4; i++) {
cnt[arr[i]]--;
check[arr[i]] = false;
}
}
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < cnt.length; i++) {
max = Math.max(max, cnt[i]);
}
boolean search = false;
for (int i = 0; i < focus.length; i++) {
if (check[focus[i]] == true && cnt[focus[i]] == max) {
System.out.println("#" + tc + " " + focus[i]);
search = true;
break;
}
}
if (!search) {
for (int i = 0; i < check.length; i++) {
if (check[i] == true) {
System.out.println("#" + tc + " " + i);
break;
}
}
}
}
}
}
2. 두 번째 풀이
import java.util.HashSet;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int n = sc.nextInt();
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < 10; i++) {
set.add(i);
}
for (int i = 0; i < n; i++) {
HashSet<Integer> call = new HashSet<>();
for (int j = 0; j < 4; j++) {
call.add(sc.nextInt());
}
String isPossible = sc.next();
if (isPossible.equals("YES")) {
set.retainAll(call);
} else {
set.removeAll(call);
}
}
for (int num : set) {
System.out.println("#" + tc + " " + num);
}
}
}
}
https://kim-oriental.tistory.com/38
[자바] Hash Set 정의 및 사용법 (추가, 삭제, 포함여부, 하나씩 출력, 리스트 변환 및 정렬)
안녕하세요, Java HashSet의 정의와 주요 기능에 관해 정리를 해보도록 하겠습니다. HashSet 자바의 HashSet은 Set 인터페이스의 구현체로, 중복이 허용되지 않는 자료구조입니다. 입력한 순서(인덱스)가
kim-oriental.tistory.com
Java HashSet 합집합, 차집합, 교집합, 부분집합
Java HashSet 합집합, 차집합, 교집합, 부분집합 코드 package pk; import java.util.HashSet; public class Test { public static void main(String[] args) { HashSet ss = new HashSet();// 1 2 3 4 HashSet ss2 = new HashSet();// 3 4 5 6 HashSet ss3 =
godog.tistory.com
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 홀수 피라미드 (D3) (1) | 2024.10.13 |
|---|---|
| [SW Expert Acacademy] 피보나치 수 분배 (D3) (0) | 2024.10.13 |
| [SW Expert Academy] 사랑의 카운슬러 (D3) (0) | 2024.10.13 |
| [SW Expert Academy] 통역사 성경이 (D3) (1) | 2024.10.12 |
| [SW Expert Academy] Digit sum (D3) (1) | 2024.10.12 |