Java/Programmers
[Lv.0]평행
GLOWWW
2023. 1. 18. 01:45
slope List를 만들어준 후, 이중반복문으로 dots들의 기울기를 구한다.
Collections.frequency()를 이용하여 1이상이라면 기울기가 같은게 존재한다는 것이다.
import java.util.*;
public class 평행 {
public static int solution(int[][] dots) {
int answer = 0;
List<Double> slope = new ArrayList<>();
for (int i = 0; i < dots.length; i++) {
for (int j = i + 1; j < dots.length; j++) {
slope.add((double)(dots[i][1] - dots[j][1]) / (dots[i][0] - dots[j][0]));
}
}
Set<Double> set = new HashSet<>(slope);
for (Double s : set) {
if (Collections.frequency(slope, s) > 1) {
answer = 1;
}
}
return answer;
}
public static void main(String[] args) {
// int[][] dots = new int[][]{{1, 4}, {9, 2}, {3, 8}, {11, 6}};
int[][] dots = new int[][]{{3,5}, {4,1}, {2,4}, {5,10}};
int result = solution(dots);
System.out.println(result);
}
}