/* 평행
* 점 네 개의 좌표를 담은 이차원 배열 dots가 다음과 같이 매개변수로 주어집니다
* [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
* 주어진 네 개의 점을 두 개씩 이었을 때,
* 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return
*
* dots result
* [[1, 4], [9, 2], [3, 8], [11, 6]] 1
* 점 [1, 4], [3, 8]을 잇고 [9, 2], [11, 6]를 이으면 두 선분은 평행
* [[3, 5], [4, 1], [2, 4], [5, 10]] 0
* 점을 어떻게 연결해도 평행하지 않습니다.
*
* 반례
* [[1, 1], [5, 1], [1, 10], [3, 10]]
*/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import java.lang.*;
public class programmer_0_95 {
static int[][] a1 = {{1, 4}, {9, 2}, {3, 8}, {11, 6}};
static int[][] a2 = {{3, 5}, {4, 1}, {2, 4}, {5, 10}};
static int[][] a3 = {{1, 1}, {5, 1}, {1, 10}, {3, 10}};
public int solution(int[][] dots) {
int answer = 0;
int idx = 0;
int[][] ar = new int[6][2];
double[] tst = new double[6];
for(int i = 0; i < dots.length; i++){
for(int j = i+1; j < dots.length; j++){
//방법 1
ar[idx][0] = Math.abs(dots[i][0] - dots[j][0]); //x
ar[idx][1] = Math.abs(dots[i][1] - dots[j][1]); //y
//방법 2
tst[idx] = (double) Math.abs(dots[i][1] - dots[j][1]) / Math.abs(dots[i][0] - dots[j][0]);
idx++;
}
}
//방법 1
for(int i = 0; i < ar.length; i++){
for(int j = i+1; j < ar.length; j++){
if(ar[i][0] == ar[j][0] && ar[i][1] == ar[j][1]) answer = 1;
}
}
//방법 2
for(int i = 0; i < tst.length; i++){
for(int j = i+1; j < tst.length; j++){
System.out.println(tst[i] + " / " + tst[j]);
if(tst[i] == tst[j]) answer = 1;
}
}
return answer;
}
public static void main(String args[]){
programmer_0_95 t = new programmer_0_95();
System.out.println("---------------------------------------");
System.out.println("result = " + t.solution(a1));
System.out.println("---------------------------------------");
System.out.println("result2 = " + t.solution(a2));
System.out.println("---------------------------------------");
// System.out.println("result3 = " + t.solution(a3));
// System.out.println("---------------------------------------");
}
}
|
cs |
방법 1로 풀 경우, 테스트케이스 1, 8에서 걸림.
이유는 모르겠다..
방법 2로 기울기 데이터 생성 후 진행시 정상적으로 통과.