• 문제 링크
6165번: Game of Lines
Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 <= N <= 200) distinct lattice points. Dot i has the integer coordinates X_i and Y_i (-1,000 <= X_i <= 1,000; -1,000 <= Y_i <= 1,000). Bessie can score a point
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
Pair[] arr = new Pair[n];
for (int i = 0; i < n; i++) arr[i] = new Pair(read(), read());
Arrays.sort(arr, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.y != o2.y ? o1.y - o2.y : o1.x - o2.x;
}
});
Set<Double> set = new HashSet<>();
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) set.add(slope(arr[i], arr[j]));
bw.write(String.valueOf(set.size()));
bw.flush();
}
private static double slope(Pair a, Pair b) {
return (double) (b.x - a.x) / (b.y - a.y);
}
private static class Pair {
int y, x;
Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
boolean flag = n == 13;
if (flag) n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return flag ? ~n + 1 : n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 16524 Database of Clients - Data Structure / Java (0) | 2023.11.10 |
---|---|
[백준] 25329 학생별 통화 요금 계산 - Data Structure / Java (0) | 2023.11.09 |
[백준] 26043 식당 메뉴 - Data Structure / Java (0) | 2023.11.07 |
[백준] 14402 야근 - Data Structure / Java (0) | 2023.11.06 |
[백준] 3982 Soccer Bets - Data Structure / Java (0) | 2023.11.05 |
댓글