평행 - 프로그래머스(자바)

강신찬·2023년 5월 18일
0
class Solution {
    public int solution(int[][] dots) {
        int answer = 0;
        int x1 = dots[0][0];
        int y1 = dots[0][1];
        int x2 = dots[1][0];
        int y2 = dots[1][1];
        int x3 = dots[2][0];
        int y3 = dots[2][1];
        int x4 = dots[3][0];
        int y4 = dots[3][1];
        if((double) Math.abs(y1-y2)/Math.abs(x1-x2) == (double)Math.abs(y3-y4)/Math.abs(x3-x4)) {
            answer = 1;
        }else if((double)Math.abs(y1-y3)/Math.abs(x1-x3) == (double)Math.abs(y2-y4)/Math.abs(x2-x4)) {
            answer = 1;
        }else if((double)Math.abs(y1-y4)/Math.abs(x1-x4) == (double)Math.abs(y2-y3)/Math.abs(x2-x3)) {
            answer = 1;
        }

        return answer;
    }
}
  • 아무리 고민해도 반복문으로 점들을 뿌려서 검증할수가 없다고 생각했다.
  • 그것은 노가다를 해야 한다는 뜻
  • 변수로 점 8개를 선언했다.
  • 대각선 각도 구하기 공식 (x1-x2)/(y1-y2)로 서로 비교해서 맞으면 평행이라는 조건을 걸었다.
  • 근데 다른 풀이 보니깐 절대값으로 안해도 되던데... 무조건 되는건가? 저게 순서 잘못되면 마이너스가 나올수도 있다고 생각해서 나는 절대값으로 바꿔준건데 흠..
  • 아무튼 한 30분 헤맸는데 그 이유는 실수로 변수타입을 변경해주지 않아서 2개의 테스트 구문에 걸렸다. 아쉽... 끝.
profile
꾸준히 공부하는 백엔드 개발자

0개의 댓글

관련 채용 정보