본문 바로가기

Java/프로그래머스

[JAVA] 프로그래머스 - 평행

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

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

 

문제 풀이 

 

[프로그래머스] 평행 파이썬 (tistory.com)

 

[프로그래머스] 평행 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/120875 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞

thingjin.tistory.com

 

- 네 개의 직선 중, 두 직선의 기울기가 하나라도 같다면 평행 조건이 지켜지게 됨 

 

 

정답 

 

import java.util.*;

class Solution {
    //기울기 
    public double gradient(int a[], int b[])
    {
        return (double)(a[1]-b[1])/(a[0]-b[0]);
    }
    
    public int solution(int[][] dots) {
        int answer = 0;
        
        int p1[] = dots[0];
        int p2[] = dots[1];
        int p3[] = dots[2];
        int p4[] = dots[3];
        
        boolean check1 = gradient(p3,p1)==gradient(p4,p2);
        boolean check2 = gradient(p4,p3)==gradient(p2,p1);
        
        if(check1 || check2)
        {
            answer = 1;
        }
        else
        {
            answer = 0;
        }
        return answer;
    }
}