프로그래머스 문제풀이/Level 0

평행 (자바, Java)

뮤츠 2022. 12. 13. 00:04

별로 특이사항 없길래 안 올리려다가, 프로그래머스 0레벨 중 정답률이 두번째로 낮아서 그냥 썼다.

평행=두 선분의 기울기가 같다 인데, 중요한건 int값은 나눗셈에서 버림처리가 될 수 있기에...double로 처리해줘야한다.

 

class Solution {
    public int solution(int[][] dots) {
        int answer = 0;
        if (slope(dots[0], dots[1])==slope(dots[2],dots[3])) {
        	answer = 1;
        } else if (slope(dots[0], dots[2])==slope(dots[1],dots[3])) {
        	answer = 1;
        } else if (slope(dots[0], dots[3])==slope(dots[1],dots[2])) {
        	answer = 1;
        }
        return answer;
    }
    
    public double slope(int[] a, int[] b) {
    	return (a[0]-b[0])*1.0/(a[1]-b[1]);
    }
}