티스토리 뷰

 

이번 문제는 너무 어렵게 생각해서 해결을 못한 문제 같다.

"더욱 간략하게 더욱 가독성있게"를 외치며 되려 간단한 코드를 더욱 어렵게 만들어 몇시간을 투자해버린 것 같다.

 

우선 문제 해결 방법은 두 점을 이루는 선의 기울기를 구하는 것이고

동일한 기울기의 선이 존재한다면 둘은 평행하게 된다.

 

이때, 4개의 점이 주어지기에 비교가 가능한 경우의 수는 3개이다.

using System;

public class Solution {
    public int solution(int[,] dots) {
        if (CalculateSlope(dots[0, 0], dots[0, 1], dots[1, 0], dots[1, 1]) == CalculateSlope(dots[2, 0], dots[2, 1], dots[3, 0], dots[3, 1]))
            return 1;
        if (CalculateSlope(dots[0, 0], dots[0, 1], dots[2, 0], dots[2, 1]) == CalculateSlope(dots[1, 0], dots[1, 1], dots[3, 0], dots[3, 1]))
            return 1;
        if (CalculateSlope(dots[0, 0], dots[0, 1], dots[3, 0], dots[3, 1]) == CalculateSlope(dots[1, 0], dots[1, 1], dots[2, 0], dots[2, 1]))
            return 1;
        return 0;
    }
    
    private double CalculateSlope(int x1, int y1, int x2, int y2) => (double)(x1 - x2) / (y1 - y2);
}

최근에 올라온 글
최근에 달린 댓글
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함