백준 1085

hong030·2023년 1월 30일
0
  • solved.ac 기준 브론즈 3단계 문제


풀이)
내가 위치한 x, y 좌표에서부터 각 경계선까지의 거리 중 가장 짧은 것을 구하기 위해선, x, y, |w-x|, |h-y| 의 최솟값을 구해야 한다.
Math 클래스 메소드를 활용하여 본 문제를 풀면 된다.

int xABS = Math.abs(x); // x의 절댓값.
int min = Math.min(a, b); // a, b 중 더 작은 값.

내 코드)


import java.util.Scanner;

public class Main{
	public static void main(String[]args){
		
		Scanner s = new Scanner(System.in);
		
		int x = s.nextInt();
		int y = s.nextInt();
		int w = s.nextInt();
		int h = s.nextInt();
		
		int wAbs = Math.abs(w-x);
		int hAbs = Math.abs(h-y);
		
		int min1 = Math.min(wAbs, hAbs);
		int min2 = Math.min(x, y);
		int min = Math.min(min1, min2);
		
		System.out.println(min);
		s.close();
	}	
}
profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글