본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 | 평행 | Java

by unknownomad 2025. 9. 26.

문제

https://school.programmers.co.kr/learn/courses/30/lessons/120875

 

풀이

class Solution {
    public int solution(int[][] dots) {
        // 점들을 꺼내서 변수에 저장
        int x1 = dots[0][0], y1 = dots[0][1];
        int x2 = dots[1][0], y2 = dots[1][1];
        int x3 = dots[2][0], y3 = dots[2][1];
        int x4 = dots[3][0], y4 = dots[3][1];
        int answer = 0;

        // 조합 1: (0,1) vs (2,3)
        double slope1 = (double) (y2 - y1) / (x2 - x1);
        double slope2 = (double) (y4 - y3) / (x4 - x3);
        if (slope1 == slope2) answer = 1;

        // 조합 2: (0,2) vs (1,3)
        slope1 = (double) (y3 - y1) / (x3 - x1);
        slope2 = (double) (y2 - y4) / (x2 - x4);
        if (slope1 == slope2) answer = 1;

        // 조합 3: (0,3) vs (1,2)
        slope1 = (double) (y4 - y1) / (x4 - x1);
        slope2 = (double) (y2 - y3) / (x2 - x3);
        if (slope1 == slope2) answer = 1;

        return answer;
    }
}

 

댓글