오늘은 백준의 실버 문제가 나왔다.
두 점을 지나는 직선의 개수를 구하는 문제인데 이해하는데 조금 시간이 걸렸다.
AI 챗봇의 도움을 받아 sudo code를 참고삼아 코드를 구현하려고 했는데, 머리에 안 들어와서 전체 코드를 참고했다.
요즘 뇌가 조금 굳었는지 머리 회전이 전만큼 안되어서 코드 분석을 중심으로 머리를 회전시켜봐야겠다!
[백준: 2358번] 평행선
🐥 제출한 코드
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
// HashMap에 각 좌표별로 점의 개수를 저장, (x가 0일 때, 하나의 점(0,y)이 몇 번 등장 했는지)
Map<Integer, Integer> xCnt = new HashMap<>(n);
Map<Integer, Integer> yCnt = new HashMap<>(n);
int total = 0;
// 각 좌표의 등장 횟수를 셈
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
xCnt.put(x, xCnt.getOrDefault(x, 0) + 1);
yCnt.put(y, yCnt.getOrDefault(y, 0) + 1);
}
// 각 좌표별 등장 횟수를 순회하며 직선 개수를 셈
// (0,0), (0,10)처럼 x = 0일 때, 2개 이상의 점을 지나야 직선으로 간주함으로 등장 횟수가 2 이상일 때 total 증가
for (int cnt : xCnt.values()) {
if (cnt >= 2) total++;
}
for (int cnt : yCnt.values()) {
if (cnt >= 2) total++;
}
System.out.println(total);
}
}
👨🏻💻 시니어 개발자 코드
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Map<Integer, Integer> xCount = new HashMap<>();
Map<Integer, Integer> yCount = new HashMap<>();
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
xCount.put(x, xCount.getOrDefault(x, 0) + 1);
yCount.put(y, yCount.getOrDefault(y, 0) + 1);
}
int result = 0;
for (int count : xCount.values()) {
if (count >= 2) result++;
}
for (int count : yCount.values()) {
if (count >= 2) result++;
}
System.out.println(result);
}
}
'코딩테스트 > [스파르타] 작심큰일 코테 챌린지' 카테고리의 다른 글
| [Day 9] TIL (2) | 2025.08.15 |
|---|---|
| [Day 8] TIL (0) | 2025.08.14 |
| [Day 6] TIL (2) | 2025.08.12 |
| [Day 5] TIL (2) | 2025.08.08 |
| [Day 4] TIL (3) | 2025.08.07 |
