본문 바로가기

코딩 테스트/백준

[백준 1000번 문제, JAVA] 네 번째 점

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
반응형