-
[백준] 1085번 : 직사각형에서 탈출 [JAVA]Algorithm/백준[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함수를 사용할 수도 있다.
'Algorithm > 백준[JAVA]' 카테고리의 다른 글
[백준] 1267번 : 핸드폰 요금 [JAVA] (0) 2024.05.05 [백준] 1247번 : 부호 [JAVA] (0) 2024.05.05 [백준] 2530번 : 인공지능 시계 [JAVA] (0) 2024.05.05 [백준] 2083번 : 럭비 클럽 [JAVA] (0) 2024.05.05 [백준] 1264번 : 모음의 개수[JAVA] (0) 2024.05.04