Algorithm
[백준][JAVA] 1085 : 직사각형에서 탈출
밍맹030
2020. 1. 7. 15:38
728x90
문제
한수는 지금 (x, y)에 있다. 직사각형의 왼쪽 아래 꼭짓점은 (0, 0)에 있고, 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램을 작성하시오.
입력
첫째 줄에 x y w h가 주어진다. w와 h는 1,000보다 작거나 같은 자연수이고, x는 1보다 크거나 같고, w-1보다 작거나 같은 자연수이고, y는 1보다 크거나 같고, h-1보다 작거나 같은 자연수이다.
출력
첫째 줄에 문제의 정답을 출력한다.
728x90
처음으로 백준 문제 한 번에 통과해서(어려운 문제도 아니지만)이상하고도 뿌듯,,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class Main{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int x=scan.nextInt();
int y=scan.nextInt();
int w=scan.nextInt();
int h=scan.nextInt();
int left=x;
int right=h-y;
int up=w-x;
int down=y;
int[] temp= {left,right,up,down};
int result=temp[0];
for(int i=0; i<temp.length;i++) if(temp[i]<result) result=temp[i];
System.out.println(result);
}
}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
728x90