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

Posted by 열정보이
2018. 12. 5. 11:26 Algorithms


쉽게 해결할 수 있는 '직사각형에서 탈출' 문제...!


그냥 x와 w를 비교한 값인 width 그리고 y와 h를 비교한 값인 height 만들고, x, y, width, height 중 가장 작은 값을 출력하면 된다!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package algorithms;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Al1085 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer token = new StringTokenizer(br.readLine());
        
        int x = Integer.parseInt(token.nextToken());
        int y = Integer.parseInt(token.nextToken());
        int w = Integer.parseInt(token.nextToken());
        int h = Integer.parseInt(token.nextToken());
        
        System.out.println(check(x,y,w,h));
    }
    /* 거리check method */
    public static int check(int x, int y, int w, int h){
        int ans = Integer.MAX_VALUE;
        
        int width = w - x;
        int height = h - y;
        
        ans = Math.min(width, height);
        ans = Math.min(ans, x);
        ans = Math.min(ans, y);
        
        return ans;
    }
}
 
cs

조금씩 더 어려운 문제에 도전해봐야겠다.