Algorithm/백준[JAVA]

[백준] 1085번 : 직사각형에서 탈출 [JAVA]

코린이 김투덜 2024. 5. 5. 18:57

# 문제

 
 

# 접근방식

  • 경계선까지의 최솟값을 구하는 것이기 때문에 w와 h를 반으로 나눠 사분면을 만든 후 어디에 위치하는지 파악하면 된다.
  • 이후에 w_min과 h_min을 비교해서 더 작은 값을 선택

 

 

# 소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int x = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        int w = Integer.parseInt(st.nextToken());
        int h = Integer.parseInt(st.nextToken());

        int w_min = 0;
        int h_min = 0;

        if(w/2 < x){
            w_min = w-x;
        }
        else {
            w_min = x;
        }

        if(h/2 < y){
            h_min = h-y;
        }
        else{
            h_min = y;
        }

        if(h_min < w_min){
            System.out.println(h_min);
        }
        else {
            System.out.println(w_min);
        }
    }
}
 

# 성능

 

# 회고

마지막 h_min과 w_min을 비교하는 if문 대신 Math.min함수를 사용할 수도 있다.