PLOD

[백준] 평행선(2358) 본문

대외 활동 및 IT 지식/알고리즘 문제 풀이 정리

[백준] 평행선(2358)

훌룽이 2026. 3. 23. 20:11

🔗 문제 링크

https://www.acmicpc.net/problem/2358

✅ 코드

package test.silver;

import java.util.*;
import java.io.*;

public class 백준_2358 {
    static int n;
    static HashMap<Integer, Integer> xMap;
    static HashMap<Integer, Integer> yMap;
    public static void main(String[] args) throws Exception{
        int answer = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());

        xMap = new HashMap<>();
        yMap = new HashMap<>();

        for(int i = 0; i < n ; i++){
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());

            xMap.put(x, xMap.getOrDefault(x, 0) + 1);
            yMap.put(y, yMap.getOrDefault(y, 0) + 1);
        }

        for (int count : xMap.values()) {
            if (count >= 2) {
                answer++;
            }
        }

        for (int count : yMap.values()) {
            if (count >= 2) {
                answer++;
            }
        }

        System.out.println(answer);
        br.close();
    }
}

💡 배운 점 & 느낀 점

StringTokenizer → 문자열을 공백단위로 이어서 입력받을 때 사용합니다(Python의 split() 과 같은 역할임)

Comments