728x90
반응형
문제
세 점이 주어졌을 때, 축에 평행한 직사각형을 만들기 위해서 필요한 네 번째 점을 찾는 프로그램을 작성하시오.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
List<Point> pointList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String[] input = bf.readLine().split(" ");
pointList.add(new Point(Integer.parseInt(input[0]), Integer.parseInt(input[1])));
}
List<Segment> segments = new ArrayList<>();
segments.add(new Segment(pointList.get(0), pointList.get(1)));
segments.add(new Segment(pointList.get(1), pointList.get(2)));
segments.add(new Segment(pointList.get(2), pointList.get(0)));
Segment segment = Collections.max(segments);
Point answer1, answer2;
answer1 = new Point(segment.p2.x, segment.p1.y);
answer2 = new Point(segment.p1.x, segment.p2.y);
for (Point p : pointList) {
if (p.x == answer1.x && p.y == answer1.y) {
System.out.printf("%d %d\n", answer2.x, answer2.y);
break;
} else if (p.x == answer2.x && p.y == answer2.y) {
System.out.printf("%d %d\n", answer1.x, answer1.y);
break;
}
}
}
static class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
static class Segment implements Comparable<Segment> {
Point p1, p2;
double length;
double angle;
Segment(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
this.length = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
this.angle = Math.toDegrees(Math.atan2(p1.y - p2.y, p1.x - p2.x));
}
@Override
public int compareTo(Segment o) {
return (this.length > o.length)? 1 : -1;
}
}
}
해결
참고
링크
728x90
반응형
'코딩 테스트 > 백준' 카테고리의 다른 글
[백준 1032번 문제, JAVA] 명령 프롬프트 (0) | 2023.03.08 |
---|---|
[백준 2477번 문제, JAVA] 참외밭 (0) | 2023.03.06 |
[백준 1100번 문제, JAVA] 하얀 칸 (0) | 2023.03.04 |
[백준 1094번 문제, JAVA] 막대기 (1) | 2023.03.03 |
[백준 1026번 문제, JAVA] 보물 (0) | 2023.03.02 |