728x90
https://swexpertacademy.com/main/solvingProblem/solvingProblem.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com

1. HashMap 사용
import java.util.HashMap;
import java.util.Map;
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++) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 3; i++) {
int num = sc.nextInt();
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
// System.out.println(entry.getValue());
if (entry.getValue() % 2 != 0) {
System.out.println("#" + tc + " " + entry.getKey());
}
}
}
}
}
2. 배열 arr 사용
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int count[] = new int[101];
for (int i = 0; i < 3; i++) {
count[sc.nextInt()]++;
}
for (int i = 0; i <= 100; i++) {
if (count[i] % 2 != 0) {
System.out.println("#" + tc + " " + i);
break;
}
}
}
}
}
3. 삼항연산자를 이용한 풀이
import java.util.Scanner;
class Main {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = (a == b ? c : (a == c ? b : a));
System.out.println("#" + tc + " " + d);
}
}
}
3번이 가장 바람직한 것 같다.
4. 비트연산자를 이용한 풀이
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 a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("#"+tc+" "+(a^b^c));
}
}
}

import java.util.ArrayDeque;
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++) {
ArrayDeque<Integer> q = new ArrayDeque<>();
q.add(sc.nextInt());
for (int i = 0; i < 2; i++) {
int num = sc.nextInt();
if (!q.isEmpty()&&num == q.peek())
q.poll();
else
q.add(num);
}
System.out.println("#"+tc+" "+q.poll());
}
}
}
큐로 짝 찾기로도 가능
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [S/W 문제해결 기본] 8일차 - 암호문3 (D3) (4) | 2024.09.02 |
|---|---|
| [SW Expert Academy] [S/W 문제해결 기본] 3일차 - String (D3) (0) | 2024.09.02 |
| [SW Expert Academy] [S/W 문제해결 기본] 5일차 - Magnetic (D3) (1) | 2024.09.01 |
| [SW Expert Academy] [S/W 문제해결 기본] 7일차 - 암호생성기 (D3) (1) | 2024.09.01 |
| [SW Expert Academy] 보충학습과 평균 (D3) (0) | 2024.08.31 |