[Programmers] Lv0 평행 Java

2023. 1. 25. 23:58CS/자료구조 & 알고리즘

728x90

문제 출처

[프로그래머스 코딩 테스트 연습]

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

문제 풀이

  • 평행하다고 하는 것은 기울기가 갔다 와 의미가 같다.
  • 기울기를 구하는 공식은 (y2 - y1) / (x2 - x1)이다.
  • 평행을 하는 경우의 수를 구하고 그 경우의 수 끼리 서로 같은지 구분하여 풀었다.

 

소스 코드

public class Solution {

    public double getGrade(int[] dot1, int[] dot2) {
        return (dot2[1] - dot1[1]) / ((double) dot2[0] - dot1[0]);
    }

    public int solution(int[][] dots) {
        double[] caseGrades = {getGrade(dots[0], dots[1]), getGrade(dots[0], dots[2]), getGrade(dots[0], dots[3]),
                                getGrade(dots[1], dots[2]), getGrade(dots[1], dots[3]), getGrade(dots[2], dots[3])};

        for (int i = 0; i < 6; i++)
            for (int j = i + 1; j < 6; j++)
                if (caseGrades[i] == caseGrades[j])
                    return 1;
        return 0;
    }
}
728x90