



내가 생각했을때 문제에서 원하는부분
The first line of the input contains the integer
$N$. Each of the next
$N$ lines contains two integers
$X_i$ and
$Y_i$, each in the range
$-10^4 \ldots 10^4$ inclusive, describing the location of a fence post.
As the area itself is not necessarily an integer,
output two times the maximum area of a valid triangle formed by the fence posts.
내가 이 문제를 보고 생각해본 부분
Coord 클래스를 정의한 이유
문제에서 점의 좌표 (x, y)를 입력받아야 하므로 편의를 위해 클래스를 사용
구조체로도 가능하지만 객체지향적 접근을 위해 클래스 사용
3중 반복문을 사용한 이유
문제의 조건대로 서로 다른 3개의 점을 고르는 모든 조합을 보기 위해
continue를 사용하여 불필요한 조합은 건너뛰기
최대 넓이 계산하는 이유
문제에서 요구하는 직사각형의 넓이를 계산하기 위함
Math.abs와 Math.max를 사용하여 절대값과 최대값 도출
배열 크기 101로 설정한 이유
문제 조건에 따라 최대 100개의 점 입력이 가능하다고 명시되어 있음
인덱스 0부터 사용하기 위해 1 더 크게 잡음
코드로 구현
package baekjoon.baekjoon_19;
import java.util.Scanner;
// 백준 18786번 문제
public class Main658 {
static class Coord {
int x, y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Coord[] p = new Coord[101];
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
p[i] = new Coord(x, y);
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
for (int k = 0; k < n; k++) {
if (i == k || j == k) continue;
if (p[i].x == p[j].x && p[i].y == p[k].y) {
ans = Math.max(ans, Math.abs(p[i].y - p[j].y) * Math.abs(p[i].x - p[k].x));
}
}
}
}
System.out.println(ans);
sc.close();
}
}

최근에 계속 영어 문제로된 알고리즘을 계속 풀고있지만 아직 많이 풀어보지 못해서 많이 어렵게 느껴지는것같다.