백준 Alex Origami Squares

KIMYEONGJUN·2024년 5월 18일
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

The single line of the input file contains two integers h and w — the height and the width of the sheet of paper (1 ≤ h, w ≤ 1000).
Output a single real number — the maximum possible length of the square side. It should be possible to cut out three such squares of h × w sheet of paper, so that their sides are parallel to the sides of the sheet.

Your answer should be precise up to three digits after the decimal point.

내가 이 문제를 보고 생각해본 부분

직사각형의 너비, 높이를 저장할 double형 일차원 배열 a, 정답을 출력할 ans를 출력한다.
직사각형을 90도 오른쪽으로 회전한다면 너비는 높이, 높이는 너비가 됩니다. 너비, 높이순으로 입력된 a[0], a[1]를 오름차순으로 정렬해준다.
앞으로 a[0]은 너비, a[1]은 높이라고 생각한다.

 너비 * 3 < 높이라면 너비로만 이루어진 정사각형을 만드는 것이 최대이므로 ans = a[0]이다.
너비 * 1.5 < 높이라면, 너비 * 2 < 높이 * 3, 높이 / 3인 a[1] / 3이 최소 3개 정사각형을 만들 수 있는 ans가 된다.
반대라면 너비 / 2가 ans가 된다.
ans를 소수점 4번째에서 반올림한 값을 출력한다.

코드로 구현

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        double[] a = new double[2];
        double ans;

        Scanner sc = new Scanner(System.in);
        a[0] = sc.nextDouble();
        a[1] = sc.nextDouble();

        Arrays.sort(a);

        if(a[0] * 3 < a[1]) {
            ans = a[0];
        } else if(a[0] * 1.5 < a[1]) {
            ans = a[1] / 3;
        } else {
            ans = a[0] / 2;
        }

        System.out.printf("%.3f", ans);
        sc.close();
    }
}

마무리

영어 문제를 이해하고 푸는데 시간이 오래 걸렸다. 아직 많이 접해보지 못해서 그런걸 수 도 있을것같다. 조금더 노력을 해야할것같다.

profile
Junior backend developer

0개의 댓글