[백준]1085_직사각형에서 탈출

김피자·2023년 1월 31일
0

백준

목록 보기
14/42

문제

한수는 지금 (x, y)에 있다. 직사각형은 각 변이 좌표축에 평행하고, 왼쪽 아래 꼭짓점은 (0, 0), 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램을 작성하시오.


입력

첫째 줄에 x, y, w, h가 주어진다.


출력

첫째 줄에 문제의 정답을 출력한다.


예제 입력

6 2 10 3
1 1 5 5 
653 375 1000 1000

예제 출력

1
1
347

풀이


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int w = sc.nextInt();
        int h = sc.nextInt();
         
        int cnt1 = ((w-x)<x? (w-x) : x);
        int cnt2 = ((h-y)<y? (h-y) : y);
        
        if(cnt1<cnt2) {
        	System.out.println(cnt1);
        }else {
        	System.out.println(cnt2);
        }
	}
}
profile
제로부터시작하는코딩생활

0개의 댓글